forked from kumaya/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortDict.py
More file actions
25 lines (18 loc) · 656 Bytes
/
Copy pathsortDict.py
File metadata and controls
25 lines (18 loc) · 656 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
dic = {'name1': 26, 'name2': 24, 'name3':202, 'name4': 1}
# Sort a dictionary on the basis of value.
# Return list of tuples in sorted order
print sorted(dic.items(), key=lambda x: x[1], reverse=False)
# Return list of keys in sorted order
print sorted(dic, key=lambda x: x[1])
# print help(dic.get)
# Reverse the dictionary and sort
print sorted(zip(dic.values(), dic.keys()))
# Sort dictionary using operator
# Return list of tuples
from operator import itemgetter
print sorted(dic.items(), key=itemgetter(1))
print sorted(dic.items(), key=itemgetter(0))
print "*"*80
# Sort
for k, v in sorted(dic.items(), key=lambda (k,v): v):
print k, v