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.
-
dict1={ 'a': 2, 'e': 3, 'f': 8, 'd': 4}
-
dict2 = sorted(dict1)
-
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
-
dict1={ 'a' : 2 , 'e' : 3 , 'f' : 8 , 'd' : 4 }
-
dict2 = sorted(dict1,reverse= True)
-
print(dict2)
-
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
-
dict1={ 'a' : 2 , 'e' : 3 , 'f' : 8 , 'd' : 4 }
-
list1= sorted(dict1.keys(),reverse=True)
-
Print (List1) # Results: [ 'F' , 'E' , 'D' , 'A' ]
3.3, sorting the values
Also, () values obtained with all dict1.values, and then sort the value
-
dict1={ 'a': 2, 'e': 3, 'f': 8, 'd': 4}
-
list1= sorted(dict1.values())
-
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
-
dict1={ 'a' : 2 , 'e' : 3 , 'f' : 8 , 'd' : 4 }
-
list1= sorted(dict1.items(),key= lambda x:x[ 1])
-
print(list1)
result:
[('a', 2), ('e', 3), ('d', 4), ('f', 8)]
Sort of bond:
-
dict1={ 'a': 2, 'e': 3, 'f': 8, 'd': 4}
-
list1= sorted(dict1.items(),key= lambda x:x[ 0])
-
print(list1)
result:
[('a', 2), ('d', 4), ('e', 3), ('f', 8)]
4 itemgetter
-
from operator import itemgetter
-
d = { "a": 8, "b": 4, "c": 12}
-
print(sorted(d.items(),key=itemgetter( 0), reverse= True))
-
print(sorted(d.items(),key=itemgetter( 1), reverse= True))
result:
-
[( 'c', 12), ( 'b', 4), ( 'a', 8)]
-
[( 'c', 12), ( 'a', 8), ( 'b', 4)]
itemgetter (0), acquiring key
itemgetter (1), to obtain value