Rioca

博客园 首页 新随笔 联系 订阅 管理

025 字典

创建和访问字典

>>> brand=['李宁','耐克','阿迪','鱼C']
>>> slogan=['一切皆有可能','Just do it','Impossible is nothing','让编程改变世界']
>>> print('鱼C工作室的口号是:',slogan[brand.index('鱼C')])
鱼C工作室的口号是: 让编程改变世界
>>> dict1={'李宁':'一切皆有可能','耐克':'Just do it','阿迪':'Impossible is nothing','鱼C':'让编程改变世界'}
>>> print('鱼C工作室的口号是:',dict1['鱼C'])
鱼C工作室的口号是: 让编程改变世界
>>> dict2={1:'one',2:'two',3:'three'}
>>> dict2
{1: 'one', 2: 'two', 3: 'three'}
>>> dict2[2]
'two'

026 字典

fromkeys()创建和访问一个新的函数

>>> dict1={}
>>> dict1.fromkeys((1,2,3))
{1: None, 2: None, 3: None}>>> dict1.fromkeys((1,2,3),'Number')
{1: 'Number', 2: 'Number', 3: 'Number'}
>>> dict1.fromkeys((1,2,3),('one','two','three'))
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> dict1.fromkeys((1,3),'girl')
{1: 'girl', 3: 'girl'}>>> dict1=dict1.fromkeys(range(32),'赞')
>>> dict1
{0: '赞', 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: '赞'}
>>> for eachKey in dict1.keys():
	print(eachKey)

	
0
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
>>> for eachValue in dict1.values():
	print(eachValue)

	
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞
赞



posted on 2020-08-02 22:27  Rioca  阅读(92)  评论(0编辑  收藏  举报