List generation, generator expression and module import in Python


5.16 list generation

for i in range(100):
  l.append('egg%s' %i)
l=['egg%s' %i for i in range(100)]
l=['egg%s' %i for i in range(1000) if i > 10]

5.17 list generation and application of generator expression

Res = map (lambda x: x.upper(), names) × map function map
names=list(res)         #['EGON', 'ALEX_SB', 'WUPEIQI', 'YUANHAO', 'LXX']
Names = [name. Upper() for name in names] (list generation)
Names = ['egon ',' Alex [sb ',' wupeiqi ',' Yunhao ',' LXX '] (list generation)
names=[len(name) for name in names if not name.endswith('sb')]
Nums = [] general loop method
with open('a.txt','r',encoding='utf-8') as f:
  for line in f:
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, 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
The execution of functions in a module is always based on the module's own namespace
read1=111111    #money=1000
spam.read1()    #def read1():
          #Print ('spam module. Read1: ', money)
#Result: spam module. Read1:1000

Alias module:

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

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
Read1() ාා񖓿ාාාා񖓿ා񖓿񖓿񖓿ා෫෫෫෫෫෫෫෫෫෫

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
Read1() ා result: spam module. Read1:1000
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
Print (money) instead of 1000

Alias module:

from spam import money as m

Import multiple in one row

from spam import money,read1,read2

6.2 two execution methods of documents:

__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. . )
   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
import m1

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


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!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!

Recommended Today

Oracle (PLSQL) introduction 1

Oracle installation, user authorization, table operation, data type, DDL table, DML data. Next: Oracle introduction 2 Learning video: Baidu cloud installation package is attached to the installation tutorial: 40774525/article/details/79919057 Oracle Baidu cloud installation package: Extraction code: 5abz After the Oracle service above is installed, open the client of SQL developer and the […]