Python基础数据类型之set集合
【摘要】 Python基础数据类型之set集合
一、set数据类型介绍
set() 函数创建一个无序不重复元素集,可进行关系测试,删除重复数据,还可以计算交集、差集、并集等。
二、set集合演示
# set集合-数据类型
m = {}
print(type(m)) # {}内为空时数据类型为字典
s = {1,2,3,3,4,"张三"}
print(s)
print(type(s)) # set集合的元素是无序的打印出来
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py
<class 'dict'>
{1, 2, 3, 4, '张三'}
<class 'set'>
Process finished with exit code 0
三、set集合中hash介绍
1.哈希定义
Hash,一般翻译做散列、杂凑,或音译为哈希,是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值。
2.数据类型的hash和不可hash
①不可哈希:python中的set集合进行数据存储的时候,需要对这些数据进行哈希计算,根据计算出的哈希值进行存储。
②可哈希的:不可变的数据类型,int, str , tuple, bool,
③不可哈希的:可变得数据类型,list,dict, set
3.set中hash示例
s = {1,2,3,3,4,"张三", []}
# print(s)
# print(type(s))
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py
Traceback (most recent call last):
File "D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py", line 17, in <module>
s = {1,2,3,3,4,"张三", []}
TypeError: unhashable type: 'list'
Process finished with exit code 1
四、set集合的插入
1.创建空集合
# s = set() # 创建空集合
# l = list()
# t = tuple()
# s = str()
2.set集合插入
s.add("张辽")
s.add("许诸")
s.add("赵云")
s.add("陆逊")
print(s)
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py
{'陆逊', '张辽', '赵云', '许诸'}
Process finished with exit code 0
五、set集合的删除
s.add("张辽")
s.add("许诸")
s.add("赵云")
s.add("陆逊")
print(s)
s.remove("张辽")
print(s)
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py
{'赵云', '陆逊', '张辽', '许诸'}
{'赵云', '陆逊', '许诸'}
Process finished with exit code 0
六、set集合的修改
s.add("张辽")
s.add("许诸")
s.add("赵云")
s.add("陆逊")
print(s)
# s.remove("张辽")
# print(s)
# 想要修改,先删除,在新增
s.remove("张辽")
s.add("孙尚香")
print(s)
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py
{'赵云', '陆逊', '许诸', '张辽'}
{'赵云', '陆逊', '孙尚香', '许诸'}
Process finished with exit code 0
七、set的查询
s.add("张辽")
s.add("许诸")
s.add("赵云")
s.add("陆逊")
print(s)
for item in s:
print(item)
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py
{'张辽', '陆逊', '赵云', '许诸'}
张辽
陆逊
赵云
许诸
Process finished with exit code 0
八、交集、并集、差集
s1 = {"python", "shell", "ansible","ruby"}
s2 = {"zabbix", "linux", "shell", "nginx"}
print(s1 & s2) #交集
print(s1.intersection(s2))
print(s1 | s2) # 并集
print(s1.union(s2))
print(s1 - s2) # 差集
print(s1.difference(s2))
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py
{'shell'}
{'shell'}
{'nginx', 'linux', 'python', 'zabbix', 'shell', 'ruby', 'ansible'}
{'nginx', 'linux', 'python', 'zabbix', 'shell', 'ruby', 'ansible'}
{'ruby', 'python', 'ansible'}
{'ruby', 'python', 'ansible'}
Process finished with exit code 0
九、set的去重作用
s1 = {"python", "shell", "ansible","ruby"}
s2 = {}
l = ["zabbix", "linux", "shell", "nginx""zabbix", "linux", "shell",
"nginx""zabbix", "linux", "shell", "nginx"]
print(l)
print(list(set(l))) # 去除重复,去重后的元素是无序的
D:\soft\python\python.exe D:/soft/pycharm/pycharmfile/py基础/02_python基础类型/13_set.py
['zabbix', 'linux', 'shell', 'nginxzabbix', 'linux', 'shell', 'nginxzabbix', 'linux', 'shell', 'nginx']
['nginx', 'shell', 'nginxzabbix', 'zabbix', 'linux']
Process finished with exit code 0
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)