Python 字典

字典

  • 类比于英语词典。python把把单词称为键(key),把它的含义成为值(value)
  • 他不是序列类型而是映射类型
  • 无序
  • 一般序列中的索引值和它所对应元素是没有关系的,所以我们引入了字典。

创建字典

brand = ['李宁','耐克','阿迪达斯']

slogan = ['一切皆有可能','Just do it','Impossible is nothing']

//可以看出0和李宁没关系,但是两个列表对应索引位置的元素有关系
//用大括号表示字典
dict1 = {'李宁':'一切皆有可能','耐克':'Just do it','阿迪达斯':'Impossible is nothing'}
dict2 = {}              //空字典
  • 使用工厂函数dict(x)—注意要传入一个映射关系
dict1 = dict( ( (1,'one') , (2,'two') , (3,'three') ) )

dict2 = dict(李宁='一切皆有可能',耐克='Just do it')
//注意关键字参数会被排序,键不能是一个表达式,不能加引号

dict2['阿迪达斯'] = 'Impossible is nothing'
//给键赋值的方式,如果不存在会自动添加,存在的话会改变原有的值。

访问字典

  • 通过dict(键)来访问它对应的值
dict1 = {1:'one',2:'two',3:'three'}
dict1[2]                            //访问
//'two'

字典中的BIF

  • fromkeys(x,[y])—创建并返回一个新的字典,x对应键值,y(可选)对应x的值,不提供默认None。
>>> dict1 = {}
>>> dict1.fromkeys((1,2,3))                                  //不传y默认为None
{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'))            //这个BIF不会智能的对应
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}

>>> dict1.fromkeys((1,3),'数字')                             //不会修改1,3的值,而是会返回一个新的字典
{1: '数字', 3: '数字'}

  • keys()—返回键的引用
  • values()—返回值的引用
  • items()—返回项的引用(元组)
  • 一起看例子
>>> dict1 = dict1.fromkeys(range(5),'赞')                          //创建五个赞
>>> dict1
{0: '赞', 1: '赞', 2: '赞', 3: '赞', 4: '赞'}  

>>> for eachkey in dict1.keys():                                   //keys()
	print(eachkey)

0
1
2
3
4

>>> for eachvalue in dict1.values():                               //values()
	print(eachvalue)
	
赞
赞
赞
赞
赞

>>> for eachitem in dict1.items():                                 //items()
	print(eachitem)
	
(0, '赞')
(1, '赞')
(2, '赞')
(3, '赞')
(4, '赞')

  • get()—访问字典中的项
>>> dict1[5]                                                       //如果我们直接访问字典中不存在的键会报错
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    dict1[5]
KeyError: 5

>>> dict1.get(5)                                                   //如果使用get(),什么都不输出。

>>> print(dict1.get(32))                                           //但是它其实是None
None

>>> dict1.get(5,'木有')                                            //我们也可以设置这个返回值,找不到返回'木有'
'木有'
>>> dict1.get(4,'木有')                                            //找到的话返回原来的值
'赞'
>>> 
  • in/not in
>>> 5 in dict1
False
>>> 4 not in dict1
False
  • clear()—清空字典
  • copy()—前拷贝,和直接赋值不同
>>> a = {1:'one',2:'two',3:'three'}
>>> b = a.copy()
>>> c = a
>>> a
{1: 'one', 2: 'two', 3: 'three'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> c
{1: 'one', 2: 'two', 3: 'three'}
>>> id(a)
92359152
>>> id(b)                                               //拷贝的地址和直接赋值的地址不同
92273472
>>> id(c)
92359152
  • pop()和popiteam()
>>> a
{1: 'one', 2: 'two', 3: 'three'}
  
>>> a.pop(2)                                           //弹出键和值
'two'
>>> a
{1: 'one', 3: 'three'}

>>> a.popitem()                                        //随机弹出一个项,因为字典无序
(3, 'three')
>>> a
{1: 'one'}
  • setdefault()—和get类似,但是如果找不到的话就自动添加
>>> a
{1: 'one'}

>>> a.setdefault(5,'five')                             //找不到自动添加
'five'
>>> a
{1: 'one', 5: 'five'}

>>> a.setdefault(1)                                    //找到返回
'one'
  • update(x)—用x来更新现有字典
>>> a
{1: 'one', 5: 'five'}

>>> b = {2:'two',3:'three',4:'four'}

>>> a.update(b)
>>> a
{1: 'one', 5: 'five', 2: 'two', 3: 'three', 4: 'four'}   //更新成功
原文地址:https://www.cnblogs.com/031602523liu/p/8654162.html