面临的任务是在字典的键key中存储文件地址,在值value中存储图片是某物的概率;之后找出概率最大的那一对儿,输出地址。
prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
}
print('')
# 按照value排序,输出(value,key)对
max_price = max(zip(prices.values(), prices.keys()))
print('max_price:', max_price) # zip反转了keys,value,所以此时按照value排序
min_price = min(zip(prices.values(), prices.keys()))
print('min_price:', min_price) # 输出(value,key)对
prices_sorted = sorted(zip(prices.values(), prices.keys()))
print('prices_sorted:', prices_sorted)
print('')
# 按照value排序,输出key
min_price_key = min(prices, key=lambda k: prices[k]) # 按value排序
max_price_key = max(prices, key=lambda k: prices[k]) # 输出key
print('min price key:', min_price_key) # FB
print('max price key:', max_price_key) # AAPL
print('sorted prices:')
prices_sorted = sorted(prices, key=lambda k: prices[k])
print(prices_sorted) # 按照股价升序排列的股票名
print('')
# 按照key排序,输出key
print('max(prices):', max(prices)) # 按照keys排序,而不是按照value
print('min(prices):', min(prices)) # 输出单一key
print('')
# 按照value排序,输出value
min_price_value = prices[min_price_key] # 最低股价
max_price_value = prices[max_price_key] # 最高股价
print('min price value:', min_price_value) # 10.75
print('max price value:', max_price_value) # 612.78