Anonymous functions and sorted Functions

Using lambda function withdrawn dictionary value: 
D = { 'A': 24, 'G': 52 is, 'I': 12 is, 'K': 33 is}
Print (List (Map (lambda X: X [. 1], d.items ())))
[24, 52 is, 12 is, 33 is]
used to sort the sort function
d_1=sorted(d.items(),key=lambda x:x[1],reverse=False)
print('d_1:',d_1)

d_1: [('i', 12), ('a', 24), ('k', 33), ('g', 52)]
 
d_2=sorted(d.items(),key=lambda x:x[1],reverse=True)
print('d_2:',d_2)
d_2: [('g', 52), ('k', 33), ('a', 24), ('i', 12)]

"" " 
In accordance with the length of each element of the ordered tuple
" ""
L = [(1,5,3), (1,3,6,3), (1,1,2,4,5,6), (1,9)]
DEF FUNC (Item):
return len (Item)
L1 = the sorted (L, FUNC = Key)
Print (L1)
[(. 1,. 9), (. 1,. 5,. 3), (. 1,. 3 ,. 6,. 3), (. 1,. 1, 2,. 4,. 5,. 6)]
L2 = the sorted (L, the lambda X = Key: len (X))
Print (L2)

[(1, 9), (1, 5, 3), (1, 3, 6, 3), (1, 1, 2, 4, 5, 6)]

It contains a dictionary list, sorted by size in ascending order elements date

s = [{ 'Date': '2018-09-04', 'test 1': '50 .00% ',' Test 2 ':' 100.00% '}, {' Date ':' 2018-09-05 ', 'test 1': '100.00%', 'test 2': 'performs no'}, { 'date': '2018-09-06', 'test 1': '100.00%', 'test 2': ' 100.00% '}, {' date ':' 2018-08-31 ',' test 1 ':' non-execution ',' test 2 ':' performs no '}, {' date ':' 2018-09-01 ',' test 1 ':' non-execution ',' test 2 ':' performs no '}, {' date ':' 2018-09-02 ',' test 1 ':' non-execution ',' test 2 ' : 'no execution'}, { 'date': '2018-09-03', 'test 1': 'non-execution', 'test 2': 'performs no'}] 
Print (the sorted (S, the lambda Key = x: x [ 'date'])) 

Print (List (Map (the lambda x: x [ 'date'], s)))

 The first print, sorted by date 

[{ ' Date ' : ' 2018-08-31 ' , ' test 1 ' : ' non-execution ' , ' Test 2 ' : ' without performing ' }, { ' Date ' : ' 2018-09-01 ' , ' test 1 ' : ' non-execution ' , ' test 2 ' : ' without performing ' }, { ' date ' : '2018-09-02', ' Test 1 ' : ' non-execution ' , ' Test 2 ' : ' without performing ' }, { ' Date ' : ' 2018-09-03 ' , ' test 1 ' : ' non-execution ' , ' Test 2 ' : ' no execution ' }, { ' date ' : ' 2018-09-04 ' , ' test 1 ' : '50.00 %', ' Test 2 ' : ' 100.00% ' }, { ' Date ' : ' 2018-09-05 ' , ' test 1 ' : ' 100.00% ' , ' Test 2 ' : ' without performing ' }, { ' date ' : ' 2018-09-06 ' , ' test 1 ' : ' 100.00% ' , ' test 2 ' :'100.00%'}]
['2018-09-04', '2018-09-05', '2018-09-06', '2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03']

Second print, taken in the dictionary using the anonymous function list a value corresponding to the date value

['2018-09-04', '2018-09-05', '2018-09-06', '2018-08-31', '2018-09-01', '2018-09-02', '2018-09-03']

  

Guess you like

Origin www.cnblogs.com/testerren/p/11410079.html