List Generator Generator Expression Module Import in python
- 2021-06-28 13:25:55
- OfStack
5.16 List Generation
l=[]
for i in range(100):
l.append('egg%s' %i)
print(l)
l=['egg%s' %i for i in range(100)]
l=['egg%s' %i for i in range(1000) if i > 10]
print(l)
5.17 List Generator and Generator Expression
names=['egon','alex_sb','wupeiqi','yuanhao','lxx']
res=map(lambda x:x.upper(),names) # map function mapping
names=list(res) #['EGON', 'ALEX_SB', 'WUPEIQI', 'YUANHAO', 'LXX']
print(names)
names=['egon','alex_sb','wupeiqi','yuanhao','lxx']
names=[name.upper() for name in names] # List Generation
print(names)
names=['egon','alex_sb','wupeiqi','yuanhao','lxx'] # List Generation
names=[len(name) for name in names if not name.endswith('sb')]
print(names)
nums=[] #1 General Cycle Method
with open('a.txt','r',encoding='utf-8') as f:
for line in f:
nums.append(len(line))
print(max(nums))
with open('a.txt','r',encoding='utf-8') as f:# List Generation
nums=[len(line) for line in f]
print(max(nums)) #28
with open('a.txt','r',encoding='utf-8') as f:
nums=(len(line) for line in f) # Generator Expressions
print(next(nums)) #15
print(next(nums)) #17
print(next(nums)) #13
print(max(nums)) #28
print(max(nums)) # Empty List
max(len(line) for line in f) # Remove brackets
Chapter 6 Modules
What is a module?A module is a collection of 1 system functions. In python, an py file is a module, such as module.py, where the module name module
6.1 import Import Module
6.11 Import Method 1
import spam
spam.read1()
Three things happened when the module was first imported &828203;1. Create a namespace for a module 2. Execute the corresponding file of the module and store the generated name in the namespace in #8203;3. Get a module name in the current executable file that points to the namespace of 1
import spam
Emphasis: Subsequent imports will refer directly to the 1 The result of the second import will not repeat the execution of the file
import spam
print(spam)
Functions in a module always execute in the module's own namespace
read1=111111 #money=1000
spam.read1() #def read1():
# print('spam Modular .read1 : ',money)
# Result: spam Modular .read1 : 1000
Alias the module:
import spam as sm
sm.read1()
engine=input('>>: ').strip()
if engine == 'mysql':
import mysql as db
else engine == 'oracle':
import oracle as db
db.parse()
One line imports multiple modules (not recommended)
import spam,mysql,oracle
6.12 Import Mode 2
from spam import money,read1,read2,change
read1()
from spam import * # Import all methods from a module
read1() #spam Medium: __all__=['money','read1'] Express * Method that can be imported, not written means that all can be imported
The first time you import a module, there are 3 things happened 1, create a module namespace 2, execute the module corresponding file, store the generated name in one of the namespace hints: from... import.... Same as import's first two things 1, get the name of the module directly in the current namespace, and use it directly without any prefix with import,Perform functions in modules, always based on the module's namespace
from spam import read1
money=1111111111
read1() # Result: spam Modular .read1 : 1000
from ... import ...... Name, get the name can be used directly without prefix, more convenient to use , The problem, however, is that it is easy to conflict with the same name as the current execution file
from spam import money
money=1111111111111111
print(money) #1111111111111111, Instead of 1000
Alias the module:
from spam import money as m
print(m)
Import multiple in one line
from spam import money,read1,read2
6.2 Two ways to execute a file:
#print(__name__)
__name__ Value of :
1 , if the file is executed directly, equal to '__main__'
2 , equal to module name when file is imported
if __name__ == '__main__':
print(' The file is executed by the script. ')
read1()
else:
print(' Files imported ')
read2()
Search Path for 6.3 Modules
The search order for modules is:
Loaded Modules in Memory ---"Built-in Modules -----" Modules included in the sys.path path
import sys
sys.path.append(r'D:\code\SH_fullstack_s1\day14\dir1')
import m1
m1.f1()
Emphasis: The first path to sys.path is the folder where the current execution file is located
summary