[Base] python list comprehensions & Dictionary derivation derivation & collection

A, defines
  one kind of unique characteristics of the python, derivation can construct another new data sequence from a data sequence structure

  List comprehension, the derivation dictionary, the set of derivation (variable data type is set, the set of elements are immutable data type)

  1, a list of derived type
    provides a simple method of generating a list of

    (1) syntax structure
      ①list = [Expression for item in the container]
      ②list = [Expression for item in the container if condition]
      ③list = [Expression for item1 in the container 1 for item2 in the container 2]

    (2) execution flow
      ① loops through each element of an Item container, the expression is applied to the front, the result of the expression to produce the final element of the list
      ② loops through each of the container elements Item, if used determining whether the condition is satisfied, the application satisfies the condition expression generating element of the new item in the list
      ③ traversed by two out of the container for loop elements, the expression element is applied to the constituent elements of the new list

1  # a nested list turn into a one-dimensional list 
2 list_num = [[l, 2,3], [4,5,6], [7,8,9 ]]
 . 3 list9 = [X for LST in list_num for X in LST]
 . 4  Print (list9)   # [. 1, 2,. 3,. 4,. 5,. 6,. 7,. 8,. 9] 
. 5  
. 6  # existing list list1 = [[1,2,3], [ 4 , 5,6], [7,8,9]] 
7  # a new list generated by the list [1,4,7] 
8  # a new list generated by the list [2,5,8] 
. 9  
10 list10 = [LST [0] for LST in list_num]
 11  Print (list10)   # [1, 4, 7]
12 
13 list11 = [lst[1] for lst in list_num]
14 print(list11)  # [2, 5, 8]  

  2, dictionary comprehensions
    is a continuation of a list comprehension of ideas, almost grammatical structure, but it ultimately produced a dictionary

    Syntax structure
      dict = {key expression: value expression for item in the container}
      dict = {key expression: value expression for item in the container if condition}
      dict = {key expression: value expression for item1 in the container 1 for item2 in the container 2}

1  # dictionary, the dictionary key, value interchangeable 
2 dict1 = { " name " : " Rose " , " Age " :. 17, " Sex " : " F " }
 . 3 dict2 = {V: K for K, V in dict1.items ()}
 . 4  Print (dict2)   # { 'Rose': 'name',. 17: 'Age', 'F': 'Sex'} 
. 5  
. 6  # used dictionaries derivation, the elements in list1 as the key, the element as the list2 value, generating a dictionary 
. 7 List1 = [ "Shinai " ," WuChengEn " , " Cao Xueqin " , " Luo Guanzhong " ]
 . 8 List2 = [ " Water Margin " , " Journey to the West " , " Dream of the Red " , " Three Kingdoms " ]
 . 9 dict3 = {X: List2 [list1.index (X)] for X in List1}
 10  Print (dict3)
 . 11 dict4 List1 = {[X]:list2[x] for x in range(len(list1))}
12 print(dict3)
13 
14 # ZIP () package, a plurality of index elements of the same iteration objects (sequence) is placed in a tuple, eventually all the tuples of a new iterable 
15 dict5 = {K: V for K, V in ZIP (List1, List2)}   # [(Shinai, Water Margin) (Journey to Journey to the West) (Cao Xueqin, Dream of the Red) (Luo Guanzhong, Three Kingdoms)] 
16  Print (dict5)

  3, the set of derivation

    Collection and derivation to derive a list of very similar formula, with the only difference is that instead of {} []

    (1) grammatical structure

      ①set = {expression for item in the container}
      ②set = {expression for item in the container if condition}
      ③set for item1 in expression = {1 for item2 in the container vessel 2}

    (2) features: automatic de-duplication function

. 1 list1 = [. 9,. 8,. 7, -2, 2, -3, -7,. 9 ]
 2  # used to derive a set of derivation elements in the set is the set of elements in square list1 
. 3 setl ** = {X 2 for X in List1}
 . 4  Print (setl) # {64,. 4,. 9, 81, 49}

Guess you like

Origin www.cnblogs.com/Tree0108/p/12110191.html