# 1.8 字典的运算

### 解决方案

``````stocks = {
"ACME": 45.23,
"AAPL": 612.78,
"IBM": 205.55,
"HPQ": 37.20,
"FB": 10.75
}
``````

``````# 取出value值最小的键值对
min_price = min(zip(stocks.values(), stocks.keys()))
print(min_price)
"""

(10.75, "FB")
"""

# 按value值的大小排序
stock_sorted = sorted(zip(stocks.values(), stocks.keys()))
print(stock_sorted)
"""

[(10.75, "FB"), (37.2, "HPQ"), (45.23, "ACME"), (205.55, "IBM"), (612.78, "AAPL")]
"""
``````

``````price_and_name = zip(stocks.values(), stocks.keys())
min_price = min(price_and_name)
print(min_price)
"""

(10.75, "FB")
"""

max_price = max(price_and_name)
print(max_price)
"""

ValueError: max() arg is an empty sequence
"""
``````

### 讨论

``````min(stocks)  # return "AAPL"
max(stocks)  # return "IBM"
``````

``````min(prices.values())  # return 10.75
max(prices.values()) # return 612.78
``````

``````min(prices, key=lambda k: prices[k]) # return "FB"
max(prices, key=lambda k: prices[k]) # return "AAPL"
``````

``````min_value = stocks[min(stocks, key=lambda k: stocks[k])] # return 10.75
max_value = stocks[max(stocks, key=lambda k: stocks[k])] # return 612.78
``````