prices = {
'GOOG': 700.4,
'ACME': 45.23,
'APLE': 112.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75,
'MSFT': 204.4
}
#value值大于200的子集。
p1 = {key: value for key, value in prices.items() if value > 200}
print(p1)
tech_names = {'IBM', 'MSFT'}
p2 = {key: value for key, value in prices.items() if key in tech_names}
print(p2)
输出:
{'GOOG': 700.4, 'IBM': 205.55, 'MSFT': 204.4}
{'IBM': 205.55, 'MSFT': 204.4}