python 容器总结整理,python容器总结
python 容器总结整理,python容器总结
python 容器总结整理
list
可变数组
tuple
不可变数组
dict
键值对(key-value)的字典(dictionary)
初始化:
a={‘lyt':90}
添加:
a[‘zxw']=91
访问:
1.a[key]
不存在会出错
2.a.get(key)
不存在返回None
3.a.get(key,val1)
不存在返回指定的val1
####判断:
>>>key in a True/False
删除:
a.pop(key)
存在返回对应的val,不存在报错
注意,key必须为不可变量,如字符串,整数,元组。不可为数组。
>>> a [1, 2, 3] >>> b (1, 2) >>> d {'lyt': 90} >>> d[a]=99 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list' >>> d[b]=99 >>> d {(1, 2): 99, 'lyt': 90}
set
不含重复key的集合
创建
需要提供一个list
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关内容
- python模拟登录并且保持cookie的方法详解,pythoncookie
- python 遍历字符串(含汉字)实例详解,python详解
- Python中shutil模块的学习笔记教程,pythonshutil
- 由浅入深讲解python中的yield与generator,pythonyield
- Python使用迭代器捕获Generator返回值的方法,pythongenera
- 使用Python对MySQL数据操作,pythonmysql数据
- 使用Python对SQLite数据库操作,pythonsqlite数据库
- python爬取w3shcool的JQuery课程并且保存到本地,w3shcooljq
- python获取当前运行函数名称的方法实例代码,python函数
- python中如何使用朴素贝叶斯算法,python贝叶斯算法
评论关闭