How Python Realizes for Loop Initialization Array with One Line of Code

  • 2021-10-15 11:08:57
  • OfStack

I won't talk too much, let's just look at the code ~


#  Use 1 Line code implementation for Loop initialization array 
o = 10
b = [ o + u for u in range( 10 ) ] 
print( b ) 
#  The result is  [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
  
x = 2
y = 3
c = [ i * y + x for i in range( 10 ) ]
print( c ) 
#  The result is  [2, 5, 8, 11, 14, 17, 20, 23, 26, 29]

Additional: Data obtained during the execution of the python for loop is stored in an array

Data structure


list=[]
for i in ... : # Loop through each of the documents 1 Row 
 .........
 line=..... #line For each 1 Secondary acquired data 
 list.append(line) # Will each 1 The acquired data is stored in the general list 
 print(list) #list Is the desired result 

Related articles: