A list generator is an expression used to generate a list in a specific syntax form . yes Python Provides a concise form of generating lists , Can quickly generate a new list.
Common grammar :[exp for iter_var in iterable]
Syntax format with filter function : [exp for iter_var in iterable if_exp]
Loop nesting syntax format : [exp for iter_var_A in iterable_A for iter_var_B in iterable_B]
such as There are the following requirements :
import string
import random
li = []
for i in range(100):
s = ''.join(random.sample(string.ascii_letters,4))
li.append(s)
print(li)
You can use the following line of code to do :
codes = [''.join(random.sample(string.ascii_letters,4)) for i in range(100)] ## The generated string Stored in a list
print(codes)
Empathy , Similar needs
# nums = []
# for num in range(1,101):
# if num%3==0:
# nums.append(num)
# print(nums)
It can still be done in one line of code
nums = [num for num in range(1,101) if num%3==0]
print(nums)
s= {i**2 for i in range(10)}
print(s)
d = {i:i**2 for i in range(10)}
print(d)
### The first method of generator implementation : Rewrite the generative expression into a generator
nums = (i**2 for i in range(1000))
print(nums) ## Output generator
# Print generation elements , Method 1 : Generate... One by one Below next Indicates build by build Say it when you use it
print(next(nums)) #0
print(next(nums)) #1
print(next(nums)) #4
print(next(nums)) #9
# Print generation elements , Method 2 :for Cycle generation
for i in nums:
print(i)
### The second part of the generator implementation 2 Methods :yield keyword
# return: Function encountered return Just go back to ,return The following code does not execute .
# yield: encounter yield Then stop executing the code , When called again next When the method is used , Will continue from where it stopped last time , encounter yield stop it .
def login():
a = 1
return 'login'
# print(1) ## return The following will not be implemented So this sentence is invalid
print(login())
def login():
print('step1')
yield 1
print('step2')
yield 2
print('step3')
yield 3
g = login()
print(next(g))
** Here is the output :**
step1
1
Its characteristics are as follows :
# 1. Functions are nested inside functions
#2. The return value of an external function is a reference to an internal function
#3. Internal functions can use variables of external functions
##1. Functions are nested inside functions
# def tiemit():
# def warpper():
# print('warpper')
# print('timeit')
# tiemit()
Output :
timeit
#2. The return value of an external function is a reference to an internal function
def timeit():
def warpper():
print('warpper')
print('timeit')
return warpper
in_fun = timeit() ## wrapper function , in_fun In essence wrapper function
in_fun()
Output :
timeit
warpper
#3. Internal functions can use variables of external functions
# def timeit(name):
# def warpper():
# print('warpper'+name)
# print('timeit')
# return warpper
# in_fun = timeit(name='mananananan') ## wrapper function , in_fun In essence wrapper function
# in_fun()
Output :
timeit
warppermananananan