012.Python的字典和集合的相关函数
Posted 战五渣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了012.Python的字典和集合的相关函数相关的知识,希望对你有一定的参考价值。
一 字典的相关函数
1.1 增函数
dictvar = {"a":1,"b":2} dictvar["c"] = 3 print(dictvar)
执行
[root@node10 python]# python3 test.py test.py {‘a‘: 1, ‘b‘: 2, ‘c‘: 3}
fromkeys()
使用一组键和默认值创建字典 (不常用 赋初始值)
listvar = [‘a‘,‘b‘,‘c‘] res = {}.fromkeys(listvar,None) print(res) res = {}.fromkeys(listvar,[1,2]) print(res)
执行
[root@node10 python]# python3 test.py test.py {‘a‘: None, ‘b‘: None, ‘c‘: None} {‘a‘: [1, 2], ‘b‘: [1, 2], ‘c‘: [1, 2]}
1.2 删函数
pop()
通过键去删除键值对 (若没有该键可设置默认值,预防报错)
dictvar = {"tiantang":"天堂","renjian":"人间","diyu":"地狱"} res = dictvar.pop("tiantang") print(res) print(dictvar) # pop可以在第二个参数上指定默认值,预防不存在改键时报错 res = dictvar.pop("ppoiiiiuiuiuiiiuiui","对不起,改键不存在") print(res)
执行
天堂 {‘renjian‘: ‘人间‘, ‘diyu‘: ‘地狱‘} 对不起,改键不存在
popitem() 删除最后一个键值对
dictvar = {"tiantang":"天堂","renjian":"人间","diyu":"地狱"} res= dictvar.popitem() print(res) print(dictvar)
执行
[root@node10 python]# python3 test.py test.py (‘diyu‘, ‘地狱‘) {‘tiantang‘: ‘天堂‘, ‘renjian‘: ‘人间‘}
clear() 清空字典
dictvar = {"tiantang":"天堂","renjian":"人间","diyu":"地狱"} dictvar.clear() print(dictvar)
执行
[root@node10 python]# python3 test.py test.py {}
1.3 更改函数
update()
批量更新(有该键就更新,没该键就添加)
dictvar = {"tiantang":"天堂","renjian":"人间","diyu":"地狱"} dictvar.update({‘tiantang‘:111,"jungle":"盘古"}) # 在括号里面写字典数据 (一个字典即可) print(dictvar) # 写法二 dictvar.update(a=1,b=2) #(在括号里面写关键字参数,是多个) print(dictvar)
执行
[root@node10 python]# python3 test.py test.py {‘tiantang‘: 111, ‘renjian‘: ‘人间‘, ‘diyu‘: ‘地狱‘, ‘jungle‘: ‘盘古‘} {‘tiantang‘: 111, ‘renjian‘: ‘人间‘, ‘diyu‘: ‘地狱‘, ‘jungle‘: ‘盘古‘, ‘a‘: 1, ‘b‘: 2}
1.4 查找函数
get()
通过键获取值(若没有该键可设置默认值,预防报错)
dictvar = {"tiantang":"天堂","renjian":"人间","diyu":"地狱"} res = dictvar.get("tiantang123") print(res) # 如果这个键不存在,可以指定默认值, 如果不写第二个参数值,默认返回None res = dictvar.get("tiantang123","对不起,该键不存在") print(res) res = dictvar[‘tiantang‘] print(res)
执行
[root@node10 python]# python3 test.py test.py None 对不起,该键不存在 天堂
keys()
将字典的键组成新的可迭代对象
dictvar = {"tiantang":"天堂","renjian":"人间","diyu":"地狱"} res = dictvar.keys() print(res) for i in dictvar.keys(): print(i) print("==============================") for i in dictvar: print(i)
执行
tiantang renjian diyu ============================== tiantang renjian diyu
values()
将字典中的值组成新的可迭代对象
dictvar = {"tiantang":"天堂","renjian":"人间","diyu":"地狱"} res = dictvar.values() print(res) for i in res: print(i)
执行
dict_values([‘天堂‘, ‘人间‘, ‘地狱‘]) 天堂 人间 地狱
items()
将字典的键值对凑成一个个元组,组成新的可迭代对象
dictvar = {"tiantang":"天堂","renjian":"人间","diyu":"地狱"}
res = dictvar.items() print(res) for i in res: print(i) print ("================================") for a,b in res: print(a,b)
执行
dict_items([(‘tiantang‘, ‘天堂‘), (‘renjian‘, ‘人间‘), (‘diyu‘, ‘地狱‘)]) (‘tiantang‘, ‘天堂‘) (‘renjian‘, ‘人间‘) (‘diyu‘, ‘地狱‘) ================================ tiantang 天堂 renjian 人间 diyu 地狱
二 集合的相关操作
作用:交差并补
2.1 intersection() 交集
set1 = {"曹操","刘禅","孙权","刘备"} set2 = {"郭嘉","刘禅","张昭","诸葛亮"} res = set1.intersection(set2) print(res) res = set1 & set2 print(res)
2.2 difference() 差集
set1 = {"曹操","刘禅","孙权","刘备"} set2 = {"郭嘉","刘禅","张昭","诸葛亮"} res = set1.difference(set2) print(res) res = set2.difference(set1) print(res) res = set1 - set2 print(res)
执行
{‘刘备‘, ‘孙权‘, ‘曹操‘} {‘郭嘉‘, ‘张昭‘, ‘诸葛亮‘} {‘刘备‘, ‘孙权‘, ‘曹操‘}
2.3 union() 并集
set1 = {"曹操","刘禅","孙权","刘备"} set2 = {"郭嘉","刘禅","张昭","诸葛亮"} res = set1.union(set2) print(res) res = set1 | set2 print(res)
执行
[root@node10 python]# python3 test.py test.py {‘孙权‘, ‘曹操‘, ‘刘备‘, ‘郭嘉‘, ‘张昭‘, ‘刘禅‘, ‘诸葛亮‘} {‘孙权‘, ‘曹操‘, ‘刘备‘, ‘郭嘉‘, ‘张昭‘, ‘刘禅‘, ‘诸葛亮‘}
2.4 symmetric_difference() 对称差集 (补集情况涵盖在其中)
set1 = {"曹操","刘禅","孙权","刘备"} set2 = {"郭嘉","刘禅","张昭","诸葛亮"} res = set1.symmetric_difference(set2) print(res) res = set1 ^ set2 print(res)
执行
[root@node10 python]# python3 test.py test.py {‘曹操‘, ‘郭嘉‘, ‘刘备‘, ‘孙权‘, ‘张昭‘, ‘诸葛亮‘} {‘曹操‘, ‘郭嘉‘, ‘刘备‘, ‘孙权‘, ‘张昭‘, ‘诸葛亮‘}
2.5 issubset() 判断是否是子集
真子集:子集元素一定少于父集,完全被包含在其中
set_father = {"曹操","刘禅","孙权","刘备"} set_son = {"曹操","刘禅"} res = set_son.issubset(set_father) print(res) res = set_son < set_father print(res) print ("==========================================") set_father = {"曹操","刘禅","孙权","刘备"} set_son = {"曹操","刘禅","孙权","刘备"} res = set_son <= set_father print(res)
执行
True True ========================================== True
2.6 判断是否是父集
set_father = {"曹操","刘禅","孙权","刘备"} set_son = {"曹操","刘禅"} res = set_father.issuperset(set_son) print(res) res = set_father > set_son print(res) print ("==========================================") set_father = {"曹操","刘禅","孙权","刘备"} set_son = {"曹操","刘禅","孙权","刘备"} res = set_son >= set_father print(res)
执行
[root@node10 python]# python3 test.py test.py True True ========================================== True
2.7 isdisjoint() 检测两集合是否不相交
不相交 True 相交False
set_father = {"曹操","刘禅","孙权","刘备"} set_son = {"曹操","刘禅"} res = set_father.isdisjoint(set_son) print(res)
执行
[root@node10 python]# python3 test.py test.py False
三 集合的相关函数
3.1 增函数
add() 向集合中添加数据
setvar = {"曹操","刘禅","孙权","刘备"} setvar.add("曹丕") print(setvar)
执行
[root@node10 python]# python3 test.py test.py {‘曹丕‘, ‘刘禅‘, ‘曹操‘, ‘刘备‘, ‘孙权‘}
update() 迭代着增加
setvar = {"曹操","刘禅","孙权","刘备"} lst = [‘曹植‘,‘曹丕‘] setvar.update(lst) print(setvar) print("==================================") setvar = {"曹操","刘禅","孙权","刘备"} setavar = "abcd" setvar.update(setvar)
print(setvar)
把列表当中的元素一个一个拿出来放进集合中,需要时可迭代性数据
执行
{‘孙权‘, ‘刘禅‘, ‘曹操‘, ‘刘备‘, ‘曹丕‘, ‘曹植‘} ================================== {‘刘禅‘, ‘孙权‘, ‘曹操‘, ‘刘备‘}
3.2 删除函数
clear() 清空集合
setvar = {"曹操","刘禅","孙权","刘备"} setvar.clear() print(setvar)
执行
[root@node10 python]# python3 test.py test.py set()
pop() 随机删除集合中的一个数据
setvar = {"曹操","刘禅","孙权","刘备"} setvar.clear() print(setvar)
执行三次
[root@node10 python]# python3 test.py test.py 刘备 {‘孙权‘, ‘曹操‘, ‘刘禅‘} [root@node10 python]# python3 test.py test.py 曹操 {‘刘备‘, ‘刘禅‘, ‘孙权‘} [root@node10 python]# python3 test.py test.py 刘禅 {‘曹操‘, ‘刘备‘, ‘孙权‘}
remove() 删除集合中指定的值(不存在则报错)
setvar = {"曹操","刘禅","孙权","刘备"} setvar.remove("刘禅") print(setvar)
执行
[root@node10 python]# python3 test.py test.py {‘曹操‘, ‘刘备‘, ‘孙权‘}
discard() 删除集合中指定的值(不存在的不删除 推荐使用)
setvar = {"曹操","刘禅","孙权","刘备"} setvar.discard("刘禅121211212") # 如果这个值不存在,就不删除,也不报错 print(setvar) setvar.discard("刘禅") # 如果这个值不存在,就不删除,也不报错 print(setvar)
执行
[root@node10 python]# python3 test.py test.py {‘刘备‘, ‘刘禅‘, ‘曹操‘, ‘孙权‘} {‘刘备‘, ‘曹操‘, ‘孙权‘}
3.3 冰冻集合
frozenset 可强转容器类型数据变为冰冻集合
冰冻集合一旦创建,不能在进行任何修改,只能做交叉并补操作
定义一个空冰冻集合
fz = frozenset() print(fz,type(fz)) fz1 = frozenset([1,"2",3,4]) fz2 = frozenset("7892") print(fz1,fz2)
执行
frozenset() <class ‘frozenset‘> frozenset({1, 3, 4, ‘2‘}) frozenset({‘9‘, ‘7‘, ‘8‘, ‘2‘})
并冻集合只能做交叉并补
fz1 = frozenset([1,"2",3,4]) fz2 = frozenset("7892") res = fz1 & fz2 print(res)
执行
[root@node10 python]# python3 test.py test.py frozenset({‘2‘})
以上是关于012.Python的字典和集合的相关函数的主要内容,如果未能解决你的问题,请参考以下文章
字符串, 列表, 元祖, 集合, 字典的相关操作和函数, 深浅copy