python built-in function zip, map, three yuan, lambda expressions

# Built-in function zip (), the plurality of iterations objects (collections, etc.) are combined into tuples tuple, stored in the object in order zip,; 
# when the parameter is empty, returns a null 
# If the zip () function compressed two lists of unequal length, then the zip () function will be subject to a shorter length list; 
list_t1 = [l, 2,3 ] 
list_t2 = [ ' Apple ' , ' Orange ' , ' Banana ' ] 
list_t3 = [50 , 60,70,80 ] 
list_t4 = (500,600,700,800 ) 

list_z1 = ZIP (list_t1, list_t2) 
list_z2 = ZIP (list_t1, list_t2, list_t3) 
list_z3 = ZIP (list_t1, list_t3) 
list_z4 = zip(list_t1,list_t3)
list_z5 = zip(list_t1,list_t4)

print(type(list_z1)) #<class 'zip'>
print(list(list_z1)) #[(1, 'apple'), (2, 'orange'), (3, 'banana')]
print(list(list_z2)) #[(1, 'apple', 50), (2, 'orange', 60), (3, 'banana', 70)]
print(list(list_z3)) #[(1, 50), (2, 60), (3, 70)]
print(list(list_z5)) #[(1, 500), (2, 600), (3, 700)]

#将两个列表转换为字典
dict_from_list = dict(zip(list_t1,list_t2))
print(dict_from_list) #. 1 {: 'Apple', 2: 'Orange',. 3: 'Banana'} 

# Map () do the mapping functions provided according to the specified sequence; 
# parameter map (func, iter, ....) , the return value ; iter is 
# calculation: for all elements within the sequence of a given calculation method, the calculation result into all returned iter; 
# the wording to be noted that, as long as the name of the method to write, and the like need not parenthesized the; 
DEF squ_minus1 (Number):
  return Number ** 2 -. 1 
tuple_test = (1,2,3,4 )
 Print (type (Map (STR, tuple_test)))   # <class 'Map'>, is understood to iterator ; 
Print (list (Map (STR, tuple_test)))   # iterator value can be converted into a list or the like for loop; 
for V in Map (STR, tuple_test):
  Print (V) 

Print (list (Map (a float, tuple_test))) #[1.0, 2.0, 3.0, 4.0] 
Print (List (Map (squ_minus1, tuple_test))) # [0,. 3,. 8, 15], a custom function calls 

# ternary operator or ternary expressions; 
# Simple to understand: when a return a true value if the return false another, similar to the else IF; 
digtal1, digtal2 = 1,2 IF digtal1> 2 :
  Print (digtal1)
 the else :
  Print (digtal2)
 # ====== \\ == Print (digtal1 IF digtal1> digtal2 the else digtal2)   # above if else directly into this formulation; 
# ternary expressions with processing of the list 
L = [1,2,3,10,20,30 ]
 # this is a conventional writing 
l_temp = []
 for

I in L:
  IF I> = 10 : 
  l_temp.append (I ** 2 )
 Print (l_temp)
 # Here is a ternary expression written 
Print (List (V ** 2 for V in L IF V> = 10 )) 

# the lambda expression; 
# the lambda expression is a function of the line. They are also known as anonymous functions in other languages. That is, the function has no specific name, but the method is created def name. 
# If you do not want to use a function in your program twice, you can use lambda expressions; 
# expressions are used: separate parameters is left, the right is the return value, ':' after only one expression, lambda functions can not be shared with other programs call; 
# , etc. or if the print statement can not be used for or lambda because lambda is just an expression; 
DEF xsqy (the X-, the y-, z):
  print (the y-the X-** - z)
 #The above function can be replaced with the following expression; 
xsqy = the lambda X, Y, Z: X ** Y - Z   # xsqy can be understood as a function of, directly behind the '=' on the left as a function name used; 
Print ( xsqy (2,3,1))

Guess you like

Origin www.cnblogs.com/juzib/p/12066927.html