About python sort of dictionary

Reprinted from https://blog.csdn.net/ustbbsy/article/details/79637594

Usually studied dictionary (dict), feeling okay. But one to use when you feel ambiguous.

So to summarize common usage dictionary, it could later be lies at the heart.

 - - - - - - - - - - - - - - - 

Update Diary: 2019-05-21

Through a statement: dictionary two parameters, the described key, value, the following keys: Key, Value: value

Welcome criticism correction!

 - - - - - - - - - - - - - - - - The following is the text - - - - - - - - - - - - - - - - 

Create a dictionary

dict1={'a':2,'b':3,'c':8,'d':4}

1, were taken key, value

Take all the keys of the dictionary, all values ​​using dict1.keys (), dict1.vaules (),

Since the key, there are many values, so to add s, additional attention here to add brackets, so pay attention to small details, it is easy to make mistakes.

print(dict1.values(),dict1.keys())

result:

dict_values([4, 2, 8, 3]) dict_keys(['d', 'a', 'c', 'b'])

As can be seen, it returns a list of

2, and take the key, the value of

While taking dictionary key, value, dict1.items (), where the same bracket and add s

print(dict1.items())

result:

dict_items([('d', 4), ('a', 2), ('c', 8), ('b', 3)])

As can be seen, the result returns a list of tuples of

That is, by dict1.items () function, a dictionary of the keys, the value of a presence tuple.

3, sorting

3.1 sorted

Look at the direct use of sorted () sort of situation.

  1.  
    dict1={ 'a': 2, 'e': 3, 'f': 8, 'd': 4}
  2.  
    dict2 = sorted(dict1)
  3.  
    print(dict2)

result:

['a', 'd', 'e', 'f']

sorted () is the default key for the dictionary, from small to large sort

3.2, to reverse the sort key

The bond reverse (descending) ordering

  1.  
    dict1={ 'a' : 2 , 'e' : 3 , 'f' : 8 , 'd' : 4 }
  2.  
    dict2 = sorted(dict1,reverse= True)
  3.  
    print(dict2)
  4.  
    Results: [ 'F' , 'E' , 'D' , 'A' ]

This sort of key as often in order to obtain a value (value)

Get the keys the maximum , the corresponding value, such as:

print(dict1[dict2[0]])#结果为8

Of course, we can first of all get the key, and then sort of key

  1.  
    dict1={ 'a' : 2 , 'e' : 3 , 'f' : 8 , 'd' : 4 }
  2.  
    list1= sorted(dict1.keys(),reverse=True)
  3.  
    Print (List1) # Results: [ 'F' , 'E' , 'D' , 'A' ]

3.3, sorting the values

Also, () values ​​obtained with all dict1.values, and then sort the value

  1.  
    dict1={ 'a': 2, 'e': 3, 'f': 8, 'd': 4}
  2.  
    list1= sorted(dict1.values())
  3.  
    Print (List1)     # Results: [2, 3, 4, 8]

Reverse = True value set reverse sequence

Can also be used dict1.items (), to give comprising key tuple values

Since the object is a tuple iteration, the return value is a list of natural tuple consisting of

Here sorting rules are defined, x means a tuple, x [. 1] is the value, x [0] is a bond

  1.  
    dict1={ 'a' : 2 , 'e' : 3 , 'f' : 8 , 'd' : 4 }
  2.  
    list1= sorted(dict1.items(),key= lambda x:x[ 1])
  3.  
    print(list1)

result:

[('a', 2), ('e', 3), ('d', 4), ('f', 8)]

Sort of bond:

  1.  
    dict1={ 'a': 2, 'e': 3, 'f': 8, 'd': 4}
  2.  
    list1= sorted(dict1.items(),key= lambda x:x[ 0])
  3.  
    print(list1)

result:

[('a', 2), ('d', 4), ('e', 3), ('f', 8)]

4 itemgetter

  1.  
    from operator import itemgetter
  2.  
    d = { "a": 8, "b": 4, "c": 12}
  3.  
    print(sorted(d.items(),key=itemgetter( 0), reverse= True))
  4.  
    print(sorted(d.items(),key=itemgetter( 1), reverse= True))

result:

  1.  
    [( 'c', 12), ( 'b', 4), ( 'a', 8)]
  2.  
    [( 'c', 12), ( 'a', 8), ( 'b', 4)]

itemgetter (0), acquiring key

itemgetter (1), to obtain value

 

Guess you like

Origin www.cnblogs.com/tianleblog/p/11590211.html