set常用方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了set常用方法相关的知识,希望对你有一定的参考价值。
一、基础使用
# 1) + * 无法使用
list01 = [1, 2, 3]
print(list01 + [4, 5, 6])
print(list01 * 3)
set01 = {11, 22, 33}
set02 = {44, 55, 66}
# print(set01 + set02)
# print(set01 * 3)
print(set01)
print(set02)
# 2) = 赋值
set01 = {11, 22, 33}
set02 = set01
print(set01)
print(set02)
# 3) len 返回元素的长度
set01 = {11, 22, 33}
print(len(set01))
# 4) in 判断是否包含元素
set01 = {11, 22, 33}
print("是否包含22:", (22 in set01))
# 5) 排序和反转,sort和reverse不支持,仅仅支持sorted
set01 = {11, 0, 22, -22, 99, 109, 33}
print(sorted(set01))
# 6) 添加元素:add
set01 = {11, 22, 33}
print(set01)
set01.add(44)
set01.add(55) # 按照哈希值顺序进行添加
print(set01)
执行结果:
C:\python\python.exe C:/python/demo/file3.py
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
{33, 11, 22}
{66, 44, 55}
{33, 11, 22}
{33, 11, 22}
3
是否包含22: True
[-22, 0, 11, 22, 33, 99, 109]
{33, 11, 22}
{33, 11, 44, 22, 55}
Process finished with exit code 0
二、删除元素
在set中删除元素的方法有:remove、pop、discard
1)remove删除指定的元素,但当指定元素不存在时,报错:KeyError。
例如:
print("删除11前:", set01)
set01.remove(11)
print("删除11后:", set01)
set02.remove(122)
print("删除一个不存在的122:", set02)
执行结果:
C:\python\python.exe C:/python/demo/file3.py
[1, 2, 3, 4, 5, 6]
Traceback (most recent call last):
[1, 2, 3, 1, 2, 3, 1, 2, 3]
{33, 11, 22}
File "C:/python/demo/file3.py", line 51, in <module>
{66, 44, 55}
set01.remove(122)
{33, 11, 22}
KeyError: 122
{33, 11, 22}
2)pop删除按照hash值存储的第一个元素,并可将移除的元素赋值给一个变量,不能指定元素移除
例如:
print("删除前:", set01)
num01 = set01.pop()
print("删除后:", set01)
# 总结:pop在删除的时候,不允许加参数,他删除是按照hash值存储的第一个元素
set02 = {77, 44, 22, 99}
print(set02)
set02.pop()
print(set02)
执行结果:
C:\python\python.exe C:/python/demo/file3.py
删除前: {0, 33, 99, -22, 108}
删除后: {33, 99, -22, 108}
{99, 44, 77, 22}
{44, 77, 22}
Process finished with exit code 0
3)discard删除指定元素,但当指定元素不存在时,不报错。
例如:
set01 = {11, 0, 22, -22, 99, 108, 33}
set02 = set01
print("删除22前:", set01)
set01.discard(22)
print("删除22后:", set01)
set02.discard(122)
print("删除一个不存在的122:", set02)
执行结果:
C:\python\python.exe C:/python/demo/file3.py
删除22前: {0, 33, 99, -22, 11, 108, 22}
删除22后: {0, 33, 99, -22, 11, 108}
删除一个不存在的122: {0, 33, 99, -22, 11, 108}
Process finished with exit code 0
三、计算:max,min,sum数值元素
set01={11,0,22,-22,99,108,33}
print(min(set01))
print(max(set01))
print(sum(set01))
四、set的逻辑运算
例如:
set01 = {11, 22, 33, 44, 55}
set02 = {44, 55, 66, 77, 88}
# 交集 (两种)
print(set01 & set02)
print(set01.intersection(set02))
# 并集(两种)
print(set01 | set02)
print(set01.union(set02))
# 差集(两种)
print(set01 - set02)
print(set01.difference(set02))
# 对等差集(两种)
print(set01 ^ set02)
print(set01.symmetric_difference(set02))
# 判断是否是子集 (set02是否是set01的子集)
print(set01 >= set02)
print(set02.issubset(set01))
# 判断是否是父集 (set01是否是set02的父集)
print(set01 <= set02)
print(set02.issuperset(set01))
执行结果:
C:\python\python.exe C:/python/demo/file3.py
{44, 55}
{44, 55}
{33, 66, 11, 44, 77, 22, 55, 88}
{33, 66, 11, 44, 77, 22, 55, 88}
{33, 11, 22}
{33, 11, 22}
{33, 66, 22, 88, 11, 77}
{33, 66, 22, 88, 11, 77}
False
False
False
False
Process finished with exit code 0
五、类型转换:set->list, set->tuple,set->str
例如:
set01 = {11, 0, 22, -22, 99, 108, 33}
list01 = list(set01)
print(type(list01))
tuple01 = tuple(set01)
print(type(tuple01))
str01 = str(set01)
print(type(str01))
print(list01)
print(tuple01)
print(str01)
执行结果:
C:\python\python.exe C:/python/demo/file3.py
<class 'list'>
<class 'tuple'>
<class 'str'>
[0, 33, 99, -22, 11, 108, 22]
(0, 33, 99, -22, 11, 108, 22)
{0, 33, 99, -22, 11, 108, 22}
Process finished with exit code 0
以上是关于set常用方法的主要内容,如果未能解决你的问题,请参考以下文章