Python -day 6

字典

定义:在Python中,字典用放在花括号{}中的一系列键-值对表示。键和值之间用冒号分隔,而键-值对之间用逗号分隔。可包含任意数量的键-值对。

访问字典中的值:可依次指定字典名和放在方括号内的键。

添加键值对:字典是一种动态结构,可随时在其中添加键-值对。可依次指定字典名、用方括号括起的键和相关联的值。

修改字典中的值:可依次指定字典名、用方括号括起的键以及与该键相关联的新值。

删除键-值对:可使用 del 语句将相应的键-值对彻底删除。删除的键-值对永远消失了。

注意:键-值对的排列顺序与添加顺序不同。Python不关心键-值对的添加顺序,而只关心键和值之间的关联关系。

 1 >>> alien_0 = {'color': 'green' , 'points': 5}
 2 >>> print(alien_0['color'])
 3 >>> print(alien_0)
 4 green
 5 {'color': 'green', 'points': 5}
 6 
 7 >>> print("Add...")
 8 >>> alien_0['x_position'] = 0
 9 >>> alien_0['y_position'] = 25
10 >>> print(alien_0)
11 Add...
12 {'color': 'green', 'points': 5, 'x_position': 0, 'y_position': 25}
13 
14 >>> print("Edit...")
15 >>> alien_0['color'] = 'yellow'
16 >>> print(alien_0)
17 Edit...
18 {'color': 'yellow', 'points': 5, 'x_position': 0, 'y_position': 25}
19 
20 >>> print("Delete...")
21 >>> del alien_0['points']
22 >>> print(alien_0)
23 Delete...
24 {'color': 'yellow', 'x_position': 0, 'y_position': 25}

遍历字典:可遍历字典所有的键-值对、键或值。

遍历字典所有的键-值对:for key, value in 字典名.items():

遍历字典中的所有键:for key in 字典名.keys():

遍历字典中的所有值:for value in 字典名.values():

按顺序遍历字典中的所有键:使用函数 sorted() 来获得按特定顺序排列的键列表的副本。

剔除字典中重复的值:使用集合 set() ,类似于列表,但每个元素都必须是独一无二的。

 1 >>> favorite_languages = {
 2 >>>     'jen': 'python' ,
 3 >>>     'sarah': 'c' ,
 4 >>>     'edward': 'ruby' ,
 5 >>>     'phil': 'python' ,
 6 >>> }
 7 >>> for key, value in favorite_languages.items():
 8 >>>     print("key: " + key)
 9 >>>     print("value: " + value)
10 >>> print("\n")
11 >>> for key in favorite_languages.keys():
12 >>>     print("Only-key: " + key)
13 >>> print("\n")
14 >>> for value in favorite_languages.values():
15 >>>     print("Only-value: " + value)
16 >>> print("\nsorted:")    
17 >>> for key in sorted(favorite_languages.keys()):
18 >>>     print("Only-key: " + key)
19 >>> for language in set(favorite_languages.values()):
20 >>>     print("set(): " + language)
21 key: sarah
22 value: c
23 key: edward
24 value: ruby
25 key: jen
26 value: python
27 key: phil
28 value: python
29 
30 Only-key: sarah
31 Only-key: edward
32 Only-key: jen
33 Only-key: phil
34 
35 Only-value: c
36 Only-value: ruby
37 Only-value: python
38 Only-value: python
39 
40 sorted:
41 Only-key: edward
42 Only-key: jen
43 Only-key: phil
44 Only-key: sarah
45 
46 set(): python
47 set(): c
48 set(): ruby

嵌套:

字典列表:将一系列的字典存储在列表中。

1 >>> alien_0 = {'color': 'green' , 'points': 5}
2 >>> alien_1 = {'color': 'yellow' , 'points': 10}
3 >>> alien_2 = {'color': 'red' , 'points': 15}
4 >>> aliens = [alien_0, alien_1, alien_2]
5 >>> for alien in aliens:
6 >>>     print(alien)
7 {'color': 'green', 'points': 5}
8 {'color': 'yellow', 'points': 10}
9 {'color': 'red', 'points': 15}

在字典中存储列表:当需要在字典中将一个键关联到多个值时,都可以在字典中嵌套一个列表。

 1 >>> favorite_languages = {
 2 >>>     'jen': ['python' , 'ruby'] ,
 3 >>>     'sarah': ['c'] ,
 4 >>>     'edward': ['ruby' , 'go'] ,
 5 >>>     'phil': ['python' 'haskell'] ,
 6 >>> }
 7 >>> for name, languages in favorite_languages.items():
 8 >>>     print("\n" + name.title() + "'s favorite languages are: ")
 9 >>>     for language in languages:
10 >>>         print("\t" + language.title())
11 Sarah's favorite languages are: 
12     C
13 Edward's favorite languages are: 
14     Ruby
15     Go
16 Jen's favorite languages are: 
17     Python
18     Ruby
19 Phil's favorite languages are: 
20     Pythonhaskell

在字典中存储字典:

 1 >>> users = {
 2 >>>     'aeinstein': {
 3 >>>         'first': 'albert',
 4 >>>         'last'" 'einstein',
 5 >>>         'location': 'princeton',
 6 >>>     },
 7 >>>     'mcurie': {
 8 >>>         'first': 'marie',
 9 >>>         'last': 'curie',
10 >>>         'location': 'paris',
11 >>>     }
12 >>> }
13 >>> for username, user_info in users.items():
14 >>>     print("\nUsername: " + username)
15 >>>     full_name = user_info['first'] + " " + user_info['last']
16 >>>     location = user_info['location']
17 
18 >>>     print("\tFull name: " + full_name.title())
19 >>>     print("\tLocation: " + location.title())
20 
21 Username: aeinstein
22     Full name: Albert Einstein
23     Location: Princeton
24 Username: mcurie
25     Full name: Marie Curie
26     Location: Paris

 

上一篇:LeetCode.953-验证外语字典顺序(Verifying an Alien Dictionary)


下一篇:Alien's Organ(泊松分布)