字典排序,求最大值等

 字典排序

from collections import OrderedDict

d = OrderedDict()
d['foo'] = 1
d['bar'] = 2
d['spam'] = 3
d['grok'] = 4
for key in d:
    print(key, d[key])
结果:

foo 1
bar 2
spam 3
grok 4

 

 

 怎样在数据字典中执行一些计算操作(比如求最小值、最大值、排序等等)?

prices = {
    'ACME': 45.23,
    'AAPL': 612.78,
    'IBM': 205.55,
    'HPQ': 37.20,
    'FB': 10.75
}
# print(lambda k: prices[k])

min_price = min(zip(prices.values(), prices.keys()))
print(min_price)
结果:
(10.75, 'FB')

 

posted @ 2019-05-13 16:34  凯文队长  阅读(491)  评论(0编辑  收藏  举报