-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.py
More file actions
47 lines (39 loc) · 828 Bytes
/
dictionary.py
File metadata and controls
47 lines (39 loc) · 828 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
36
37
38
39
40
41
42
43
44
45
46
47
#字典
alien = {'color':'green','points':5}
print(alien)
print(alien['color'])
#添加键值对
alien['x_position'] = 0
alien['y_position'] = 25
print(alien)
#修改字典的值
alien['color'] = 'yellow'
print(alien)
#删除键值对
del alien['points']
print(alien)
#遍历索引键值对
user = {
'username':'efermi',
'firname':'enrico',
'lastname':'fermi',
}
for key,value in user.items():
print("\nkey:"+key)
print("value:"+value)
#遍历所有键
for key in user.keys():
print("\n key:" + key)
#遍历所有值
for value in user.values():
print("\n value:"+value)
#去重
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
print("the following language have been mentioned")
for language in set(favorite_languages.values()):
print("language is :" + language)