a={}
a={"a":1,"b":2,"c":3,"d":4}
print(a)
print(a.items())
print(a.values())
print(list(a.items())) #以列表形式显示字典所有的键
print(list(a.values())) #以列表形式显示字典所有的值
aa={"a":1,"b":2,"c":3,"d":2}
ab=sorted(aa.items(),reverse=True)#按字典的健降序排列
ac=sorted(aa.values(),reverse=True)#按字典的值降序排列
print(ab)
print(ac)
ad=sorted(aa.items(),key=lambda item:item[1])
print(ad)
for i in ad:
print(i[0]) for i in ad:
print(i[1])
运行结果如下:
[('d', 2), ('c', 3), ('b', 2), ('a', 1)]
[3, 2, 2, 1]
[('a', 1), ('b', 2), ('d', 2), ('c', 3)]
a
b
d
c
1
2
2
3