Example 1:
Requirement: Cycle 0-10, add each value together to print
# Literary Youth Edition n = list (range) for index, I in Enumerate (n): n[i] + = 1print (n) [1, 2, 3, 4, 5, 6, 7, 8, 9, ten] # Execute Result # forced Youth n = list (range) print (list (map (lambda x:x+1, N)) [1, 2, 3, 4, 5, 6, 7, 8, 9, ten] # execution Result # more Forced Youth Edition (list-generated) n = [I+1 for I in Range]]print (n) [1, 2, 3, 4, 5, 6, 7, 8, 9, ten] # Execution results
List generation function is to implement the operation of the list in a row, where a can put anything, such as strings, dictionaries, ganso, etc., I can do ordinary operations, can also do ternary operations, for example, to achieve i< 5, nothing to do, if I >5, myself by myself
n = list (range) A = [I if I < 5 else i*i for I in N]print (a) [0, 1, 2, 3, 4,, $, A,, Bayi] #执行结果
n = ' alex ' n = [i-I in range]]print (n)
python-function Advanced-list generation, generator,