1 对键排序
(1)方法一
遍历tutle
d = {1:'我',2:'是',3:'帅',4:'哥'}
# reverse = True:从大到小排序
# reverse = Fale:从小到大排序
tt = sorted(d.items(),key = lambda kv:kv[0],reverse = True)
for t in tt:
print('{}:{}'.format(t[0],t[1]))
4:哥
3:帅
2:是
1:我
(2)方法二
遍历List
d = {1:'我',2:'是',3:'帅',4:'哥'}
# reverse = True:从大到小排序
# reverse = Fale:从小到大排序
tlist = list(d.items())
tlist.sort(key = lambda x:x[0],reverse = True)
for i in range(len(tlist)):
a,b = tlist[i]
print('{}:{}'.format(a,b))
4:哥
3:帅
2:是
1:我
2 对值排序
(1)方法一
遍历tutle
d = {'我':1,'是':2,'帅':10,'哥':9}
# reverse = True:从大到小排序
# reverse = Fale:从小到大排序
tt = sorted(d.items(),key = lambda kv:kv[1],reverse = True)
for t in tt:
print('{}:{}'.format(t[0],t[1]))
帅:10
哥:9
是:2
我:1
(2)方法二
遍历List
d = {'我':1,'是':2,'帅':10,'哥':9}
# reverse = True:从大到小排序
# reverse = Fale:从小到大排序
tlist = list(d.items())
tlist.sort(key = lambda x:x[1],reverse = True)
for i in range(len(tlist)):
a,b = tlist[i]
print('{}:{}'.format(a,b))
帅:10
哥:9
是:2
我:1