10字典dict

#字典的初始化,可以使用原表的形式进行初始化
>>> dict3 = dict((('b',1), ('e',2), ('y',3), ('o',4), ('n',5), ('d',5)))
>>> dict3
{'y': 3, 'o': 4, 'n': 5, 'b': 1, 'e': 2, 'd': 5}
>>> 
#使用键值方式初始化,键不能为字符串,
>>> dict3 = dict(beyond = 'firest', beyond1 = 'secondary')
>>> dict3
{'beyond': 'firest', 'beyond1': 'secondary'}
>>> 
>>>> dict3 = dict('beyond' = 'firest', 'beyond1' = 'secondary')
SyntaxError: keyword can't be an **expression**
>>> 
#修改已知键的值,当不存在该键时,直接插入该数据
>>> dict3['beyond1'] = 'this is change'
>>> dict3
{'beyond': 'firest', 'beyond1': 'this is change'}
>>> 
--------------------------------------------------------
#查看字典支持的方法
>>>dir(dict)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>> 
#使用第二个参数为key创建值,注意,fromkeys,第二个参数为一个values
>>> dict1.fromkeys((1,2,3,4),'one')
{1: 'one', 2: 'one', 3: 'one', 4: 'one'}
>>> 
>>> dict1.fromkeys((1,2,3,4),('one','two'))
{1: ('one', 'two'), 2: ('one', 'two'), 3: ('one', 'two'), 4: ('one', 'two')}

#遍历keys
>>> dict1 = dict.fromkeys(range(3),'one')
>>> dict1
{0: 'one', 1: 'one', 2: 'one'}
>>>
>>>> for keys in dict1.keys():
    print(keys)


0
1
2
#遍历values
>>> for values in dict1.values():
    print(values)


one
one
one
>>> 
#遍历items
>>> for items in dict1.items():
    print(items)


(0, 'one')
(1, 'one')
(2, 'one')
>>> 
#访问不存在的键时,会报错,用户体验不好,此时使用get方法进行解决
>>> dict1[4]
Traceback (most recent call last):
  File "<pyshell#102>", line 1, in <module>
    dict1[4]
KeyError: 4
>>> dict1.get(4)   #返回none
>>> dict1.get(4,'') #自定义输出结果
''
#使用setdefault,将不存在的键,插入到字典
>>> dict1.setdefault(4,'four')
'four'
>>> dict1
{0: 'one', 1: 'one', 2: 'one', 4: 'four'}
>>> 
#pop指定键值 
>>> dict1.pop(1)
'one'
>>> dict1
{0: 'one', 2: 'one', 4: 'four'}
>>> 
#字典为无序的列表,随机弹出键值
>>> dict1.popitem()
(0, 'one')
>>> dict1
{2: 'one', 4: 'four'}
>>> 
>----------------------------------------
#字典的清除
>>> dict1 = dict.fromkeys(range(4),'one')
>>> dict1
{0: 'one', 1: 'one', 2: 'one', 3: 'one'}
>>> a = dict1
>>> a    #a与dict1存有相同的数据
{0: 'one', 1: 'one', 2: 'one', 3: 'one'}
>>> 
>>> a = {}   #使用空字典对a进行初始化,此时dict1还存在数据
>>> a
{}
>>> dict1
{0: 'one', 1: 'one', 2: 'one', 3: 'one'}
>>> 
>>> 
>>> 
>>> #使用clear,清除所有相关的字典
>>> 
>>> a
{}
>>> a = dict1
>>> a
{0: 'one', 1: 'one', 2: 'one', 3: 'one'}
>>> 
>>> dict1
{0: 'one', 1: 'one', 2: 'one', 3: 'one'}
>>> 
>>> 
>>> dict1.clear()
>>> a
{}
>>> dict1
{}
>>> 
------------------------------
>>> a = dict.fromkeys(range(5),'two')
>>> a
{0: 'two', 1: 'two', 2: 'two', 3: 'two', 4: 'two'}
>>> b = a
>>> b
{0: 'two', 1: 'two', 2: 'two', 3: 'two', 4: 'two'}
>>> 
>>> c = a.copy()
>>> c
{0: 'two', 1: 'two', 2: 'two', 3: 'two', 4: 'two'}
>>> #a, b, c存有相同的数据,但是a,b引用相同数据,而c为不同数据
>>> id(a)
16891416
>>> id(b)
16891416
>>> id(c)
16863800
>>> #id(a),id(b)指向相同地方
>>> 
>----------update--------------------
>>>> a.setdefault(6)  #将6键插入到字典
>>> a
{0: 'two', 1: 'two', 2: 'two', 3: 'two', 4: 'two', 5: 'none', **6: None**}
>>> b = {6:'six'} #设置6的值,定义为字典
>>> a.update(b)  #使用字典b更新字典a
>>> a
{0: 'two', 1: 'two', 2: 'two', 3: 'two', 4: 'two', 5: 'none', **6: 'six'**}
>>> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值