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
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江湖有缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值