from collections import OrderedDict
d = OrderedDict()
d['z'] = 5
d['h'] = 3
d['a'] = 1
d['n'] = 4
d['g'] = 2
print("原始字典元素:")
for key in d:
print(key, d[key])
print("值最小的字典元素:")
min_value_order = min(zip(d.values(), d.keys()))
print(min_value_order)
print("值最大的字典元素:")
max_value_order = max(zip(d.values(), d.keys()))
print(max_value_order)
print("排序后的字典元素:")
sort_value = sorted(zip(d.values(), d.keys()))
print(sort_value)
print("排序后的降序字典元素:")
sort_value_2 = sorted(zip(d.values(), d.keys()), reverse=True)
print(sort_value_2)
print("字典最小的key")
print(min(d, key=lambda k: d[k]))
print("字典最大的key")
print(max(d, key=lambda k: d[k]))
输出:
原始字典元素:
z 5
h 3
a 1
n 4
g 2
值最小的字典元素:
(1, 'a')
值最大的字典元素:
(5, 'z')
排序后的字典元素:
[(1, 'a'), (2, 'g'), (3, 'h'), (4, 'n'), (5, 'z')]
排序后的降序字典元素:
[(5, 'z'), (4, 'n'), (3, 'h'), (2, 'g'), (1, 'a')]
字典最小的key
a
字典最大的key
z