-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.17.py
More file actions
35 lines (29 loc) · 869 Bytes
/
1.17.py
File metadata and controls
35 lines (29 loc) · 869 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 从字典中提取子集
# 字典推导式(dictionary comprehension)
prices = {
'ACME': 45.23,
'AAPL': 612.78,
'IBM': 205.55,
'HPQ': 37.20,
'FB': 10.75
}
# Make a dictionary of all prices over 200
p1 = {key: value for key, value in prices.items() if value > 200}
print(p1)
# Make a dictionary of tech stocks # 选出科技股
tech_name = {'AAPL', 'IBM', "HPQ", 'MSFT'}
p2 = {key: value for key, value in prices.items() if key in tech_name}
print(p2)
# 创建元祖序列 # 慢
p1 = dict((key, value) for key, value in prices.items() if value > 200)
print(p1)
# 慢
tech_name = {'AAPL', 'IBM', 'HPQ', 'MSFT'}
p2 = {key: prices[key] for key in prices.keys() & tech_name}
print(p2)
'''
{'AAPL': 612.78, 'IBM': 205.55}
{'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.2}
{'AAPL': 612.78, 'IBM': 205.55}
{'IBM': 205.55, 'HPQ': 37.2, 'AAPL': 612.78}
'''