# List generation, generator expression and module import in Python

Time：2019-11-4

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 generation and application of generator expression

``````names=['egon','alex_sb','wupeiqi','yuanhao','lxx']
Res = map (lambda x: x.upper(), names) × map function map
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 ',' Yunhao ',' LXX '] (list generation)
names=[len(name) for name in names if not name.endswith('sb')]
print(names)
Nums = [] general loop 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 expression
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)``````

Chapter 6 modules

What is a module Module is a collection of system functions. In Python, a py file is a module, such as module.py, in which module name is module

6.1 import module

6.11 import mode 1

``````
import spam

Three things happen when importing a module for the first time: 1. Create a module’s namespace; 2. Execute the module’s corresponding file. Store the generated name in the namespace of 1; 3. Get a module name in the current execution file. The module name points to the namespace of 1

``````import spam
Note: subsequent imports will directly refer to the results of the first import and will not execute the file repeatedly
import spam
print(spam)
The execution of functions in a module is always based on the module's own namespace
#Print ('spam module. Read1: ', money)

Alias module:

``````
import spam as sm
engine=input('>>: ').strip()
if engine == 'mysql':
import mysql as db
else engine == 'oracle':
import oracle as db
db.parse()``````

Import multiple modules in one row (not recommended)

`import spam,mysql,oracle`

6.12 import mode 2

``````from spam import money,read1,read2,change
From spam import * ා import all methods from the module

There are three things that happen in the first import module. 1, create a namespace 2 of the module, execute the corresponding file of the module, and put the generated name in the namespace hint of 1: from… Import… It is exactly the same as the first two things in import. 3, in the current namespace, get the name of the module directly, it can be used directly, without adding any prefix with import, and executing the module. The function of is always based on the module’s namespace

``````from spam import read1
money=1111111111
From... Import... Name. The name you get can be used directly without prefix, which is more convenient to use, but the problem is that it is easy to conflict with the same name in the current execution file
from spam import money
money=1111111111111111

Alias module:

``````
from spam import money as m
print(m)``````

Import multiple in one row

`from spam import money,read1,read2`

6.2 two execution methods of documents:

``````#print(__name__)
__Value of Name:
1. If the file is executed directly, it is equal to 'ᥤ main ᥤ
2. When the file is imported, it is equal to the module name
​
if __name__ == '__main__':
Print ('File is executed by the script in it. . )
else:
Print ('File imported ')

6.3 search path of module

The search order of modules is:

Modules loaded in memory built in modules included in sys.path path path

``````
import sys
sys.path.append(r'D:\code\SH_fullstack_s1\day14\dir1')
​
import m1
m1.f1()``````

Note: the first path of sys.path is the folder where the current execution file is located

summary

The above is the list generation, generator expression and module import in Python introduced by Xiaobian to you. I hope it will help you. If you have any questions, please leave a message to me, and Xiaobian will reply to you in time. Thank you very much for your support of the developepaer website!