鱼C论坛

 找回密码
 立即注册
查看: 2006|回复: 0

[技术交流] Python入门:L26字典2

[复制链接]
发表于 2017-7-9 20:56:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
当在序列中访问一个不存在的位置时,会报错越界,在字典中会创建一个新的位置。
字典是工厂函数(类型)
Fromkeys(…)
-dict.fromkeys(S[,v]  -->New dict with keys from S and values equal to v (v defaults to None))
fromkeys(iterable, value=None, /) method of builtins.type instance
    Returns a new dict with keys from iterable and values equal to value.

>>> 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')) 注意:此方法并不能 使v中的值逐个分配到对应的key中,key只能有value一个值
{1: ('one', 'two', 'three'), 2: ('one', 'two', 'three'), 3: ('one', 'two', 'three')}
>>> dict1.fromkeys((1,3),'shuzi'):此处视图修改key1和3的值,但是并不可行,python直接创建一个新的字典
{1: 'shuzi', 3: 'shuzi'}

访问字典的几个方法:
keys(), values(), items()
dict1=dict1.fromkeys(range(32),'zan')
>>> dict1
{0: 'zan', 1: 'zan', 2: 'zan', 3: 'zan', 4: 'zan', 5: 'zan', 6: 'zan', 7: 'zan', 8: 'zan', 9: 'zan', 10: 'zan', 11: 'zan', 12: 'zan', 13: 'zan', 14: 'zan', 15: 'zan', 16: 'zan', 17: 'zan', 18: 'zan', 19: 'zan', 20: 'zan', 21: 'zan', 22: 'zan', 23: 'zan', 24: 'zan', 25: 'zan', 26: 'zan', 27: 'zan', 28: 'zan', 29: 'zan', 30: 'zan', 31: 'zan'}
>>>
for eachKey in dict1.keys():
        print(eachKey)
for eachKey in dict1.values():
        print(eachKey)
for eachKey in dict1.items():
        print(eachKey)

当索引一个字典中不存在的键的时候,就会出错
>>> print(dict1[31])
zan
>>> print(dict1[32])
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    print(dict1[32])
KeyError: 32
>>>
get方法采用了更宽松的方式去访问字典的项
>>> dict1.get(32)
>>> print(dict1.get(32))
None
如果需要在不存在该项的情况下返回一个值,可以预设一个值
>>> dict1.get(32,'muyou')
'muyou'
>>> dict1.get(31,'muyou')
'zan'
判断一个键是否在字典中用成员关系操作符
>>> 31 in dict1
True
>>> 32 in dict1
False
在字典中检查键的成员资格比序列更加高效,当数据规模比较大的时候,差距会相当明显。
在字典中查找的是元素的键而不是值,在序列中查找的是值而不是索引号
Clear()
>>> dict1
{0: 'zan', 1: 'zan', 2: 'zan', 3: 'zan', 4: 'zan', 5: 'zan', 6: 'zan', 7: 'zan', 8: 'zan', 9: 'zan', 10: 'zan', 11: 'zan', 12: 'zan', 13: 'zan', 14: 'zan', 15: 'zan', 16: 'zan', 17: 'zan', 18: 'zan', 19: 'zan', 20: 'zan', 21: 'zan', 22: 'zan', 23: 'zan', 24: 'zan', 25: 'zan', 26: 'zan', 27: 'zan', 28: 'zan', 29: 'zan', 30: 'zan', 31: 'zan'}
>>> dict1.clear()
>>> dict1
{}
达到清空的效果,使用直接赋一个空字典是不严谨的
>>> a={'name':'xiaojiayu'}
>>> b=a
>>> b
{'name': 'xiaojiayu'}
>>> a={}
>>> a
{}
>>> b
{'name': 'xiaojiayu'}
浅拷贝_.copy()
>>> a={1:'one',2:'two',3:'three'}
>>> b= a.copy()
>>> c=a
>>> c
{1: 'one', 2: 'two', 3: 'three'}
>>> a
{1: 'one', 2: 'two', 3: 'three'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}
>>> id(a)
54740288
>>> id(b)浅拷贝后的数据地址与原字典地址不一样,不随原字典变化
54740336
>>> id(c)
54740288
>>> c[4]='four'
>>> c
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> a
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
>>> b
{1: 'one', 2: 'two', 3: 'three'}

>>> dir(dict)
['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__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']

_.pop(key) 从字典里弹出制定键的值
_.popitem() 随机从字典里弹出一个数据
>>> a.pop(2)
'two'
>>> a
{1: 'one', 3: 'three', 4: 'four'}
>>> a.popitem()
(4, 'four')
setdefault 跟get方法比较类似,但是在当找不到对应的时候会进行添加
>>> a.setdefault('xiaobai')
>>> a
{1: 'one', 3: 'three', 'xiaobai': None}
>>> a.setdefault(5,'five')
'five'
>>> a
{1: 'one', 3: 'three', 'xiaobai': None, 5: 'five'}
>>>
update方法是用一个字典或者映射关系去更新一个字典
>>> b={'xiaobai':'dog'}
>>> a.update(b)
>>> a
{1: 'one', 3: 'three', 'xiaobai': 'dog', 5: 'five'}

评分

参与人数 2荣誉 +3 鱼币 +4 收起 理由
康小泡 + 1
小甲鱼 + 3 + 3 支持楼主!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 01:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表