按顺序获取key的值、按key和value顺序获取字典
按顺序获取key的值
data = {'a': 7, 'c': 10, 'b': 9}
key = []
for i in sorted(data):
key.append(i[0])
print(key)
按key顺序获取字典
dic = {}
dict_num = {1: 2, 14: 45, 6: 12, 7: 23}
res = sorted(dict_num.items(), key=lambda kv: kv[0])
print(res)
for k in res:
dic[k[0]] = k[1]
print(dic)
按value顺序获取字典
d = {'red': 25, 'blue': 21, 'black': 32, 'yellow': 19}
dic = {}
res = sorted(d.items(), key=lambda item: item[1])
for k in res:
dic[k[0]] = k[1]
print(dic)