python basis - list comprehensions

Let me first say a nonsense:

At first I was a book entry through the python: "Head First Python"

Remember that for the completion of the cycle, the book describes a simplified method for the code, which data can be removed from the list by a line of code is then appended to another list, yes, that is a list of derivation formula

But at the time after school, has been thought up by the work encountered in some cases need to traverse the access directly with the cycle for, a not enough, directly on the two ~ ~
recently when writing the script, because he has been with a for loop, the code looks a bit jumbled, so suddenly think of it is not to use list comprehensions instead

OK, complete nonsense, let's start into the topic, look at the list comprehension in the end is what the hell?

1, create a list and then add the loop for use in a data list to another list

heroes = [ " Arthur " , " Daji " , " Tachibana Ukyo " , " Ah Ke " , " Wang Zhaojun " , " Ma " ]   # original list 

# Create a new list hero_name, respectively, the value of the heroes in the appended thereto 

# 1, for loop-implemented method + append 
hero_name = []
 for Hero in Heroes: 
    hero_name.append (Hero) 
Print (hero_name)

2, to use the list to add data to derive a new list

# 2, derived using the list, the data taken out of heroes into a new list 
hero_name = [Hero for Hero in heroes]
 Print (hero_name)

Print results are as follows

 3, with the if statement in the list derivation formula

# Binding if statement, for example, take out the name of a length value greater than 2 
hero_name = [Hero for Hero in Heroes if len (Hero)> 2 ]
 Print (hero_name)

Print results are as follows

 4, to see a more complex example, two cycles for how to convert to derive a list of formula

= nums [[. 1,. 3,. 5,. 7], [2,. 4,. 6,. 8 ]] 

# so that the figures are nums each taken out into a new list, used for two cycles 
new_nums = []
 for nums_a in the nums:
     # Print (nums_a) 
    for I in nums_a: 
        new_nums.append (I) 
Print ( " new_nums values: {} " .format (new_nums)) 

# list comprehension 
new_num = [I for nums_a in the nums for I in nums_a]
 # from left to right, respectively, the outer loop to the inner loop; 2 inside the first traverse over a small list, then the list traversal each small numbers; 
# final results are expressed variables, writing in the left-most 

print( " New_num values: {} " .format (new_num)) 

# add a determination condition, if more than 6, it is taken out 
new_num_1 = [I for nums_a in the nums for I in nums_a IF I> 6 ]
 Print ( " new_num_1 values: {} " .format (new_num_1)) 

# define a function, squaring 
DEF square (X):
     return X * X 

new_num_2 = [square (I) for nums_a in the nums for I in nums_a]   # the results pass to a function
Print ( " new_num_2 values: {} " .format (new_num_2))

Print results are as follows

 

The original book of small tips

 

 

Guess you like

Origin www.cnblogs.com/hanmk/p/12003451.html