7.python3 practical advanced programming skills (b)

1.5. How the value of the size of the dictionary, the dictionary of the sorting items

The first method: list comprehensions

# 1.5 how the value of the size of the dictionary, the dictionary entries are sorted 

from Random Import the randint 

D = {K: the randint (60, 100) for K in  ' ABCDEFG ' }
 Print (D)
 # The first method: using the list or parse zip () function, the dictionary keys and values reversed 
List1 = [(V, K) for K, V in d.items ()]
 # or using zip () function 
# List2 = list (ZIP (d.values (), d.keys ())) 
Print (List1) 
List1 = the sorted (List1, Reverse = True)
 Print (List1)

The second use of sorted

# 1.5 how the value of the size of the dictionary, the dictionary entries are sorted 

from Random Import the randint 

D = {K: the randint (60, 100) for K in  ' ABCDEFG ' }
 Print (D)
 # The first method: using the list or parse zip () function, the dictionary keys and values reversed 
List1 = [(V, K) for K, V in d.items ()]
 # or using zip () function 
# List2 = list (ZIP (d.values (), d.keys ())) 

List1 = sorted (List1, Reverse = True) 


# The second method: sorted using the sort 
P = sorted (d.items (), Key = the lambda Item: Item [ . 1], Reverse = True)
 Print(P)      # [( 'A', 97), ( 'B', 93), ( 'D', 93), ( 'E', 92), ( 'F', 76), ( 'C', 66), ( 'G', 61 is)] 

# fractional add a rank 
D = {K: (I, V) for I, (K, V) in the enumerate (P,. 1 )}
 Print (D)     # { ' g ': (1, 97) ,' d ': (2, 92),' f ': (3, 91),' c ': (4, 79),' a ': (5, 78),' e ': (6, 67) ,' b ': (7, 64)}

1.6. How statistical frequency of the elements in the sequence

# 1.6 How statistical frequency of elements in a sequence 
from Random Import the randint
 from Collections Import Counter 

Data = [the randint (l, 5) for _ in Range (1,20 )]
 Print (Data)    # [. 5, 2,. 1, 2 , 5, 3, 1, 1, 1, 4, 2, 5, 3, 2, 3, 5, 1, 2, 5] 

# is calculated with the highest frequency is the number of three 
C = Counter (Data)
 Print (C .most_common (. 3))     # [(. 1,. 5), (. 3,. 4), (2,. 4)]

1.7. How quickly find more dictionaries public key

# 1.7 How to find more dictionaries public key 

from Random Import randint, the Sample
 from functools Import the reduce 

d1 = {k: randint (1, 4) for k in the Sample ( ' ABCDEFGH ' , randint (3, 6 )) } 
D2 = {K: the randint (l, 4) for K in Sample ( ' ABCDEFGH ' , the randint (3,6 ))} 
D3 = {K: the randint (l, 4) for K in Sample ( ' ABCDEFGH ' , the randint (3,6 ))} 

#1. dictionary keys () method, to obtain a set of dictionary keys 
# 2 using the map function, to obtain a set of keys each dictionary 
# 3. reduce, taking the intersection of the set of all dictionary keys 

DL = [D1, D2, D3]
 # find three identical dictionary Keys 
Result = the reduce ( the lambda a, B: a & B, Map (dict.keys, DL))
 Print (Result)

 1.8. How to make dictionary to keep order

# 1.8. Dictionary how to maintain orderly 

from Collections Import OrderedDict
 from the itertools Import islice 

D = OrderedDict () 
D [ ' E ' ] =. 5 
D [ ' D ' ] =. 4 
D [ ' C ' ]. 3 = 
D [ ' B ' ] = 2 
D [ ' A ' ] =. 1 Print (D)     # OrderedDict ([(' E ',. 5), (' D ',. 4), (' C ',. 3), (' B ', 2 ), ( 'a',. 1)]) # OrderedDict dictionary, the iteration operation, it will preserve the ordering of the elements are inserted def





query_by_order (D, A, B = None):
     IF B IS None: 
        B = A +. 1
     return List (islice (D, A, B)) 

# fifth Key 
RES1 = query_by_order (D,. 4 )
 Print (RES1)      # [ 'A'] 

# of the second and third Key 
RES2 = query_by_order (D, l, 3 )
 Print (RES2)      # [ 'D', 'C']

 

Guess you like

Origin www.cnblogs.com/derek1184405959/p/11355232.html