Python学习第三十四天—集合(下)

[复制链接]
15271953841 发表于 2024-2-27 06:33:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
本帖最后由 15271953841 于 2024-2-27 06:40 编辑

Python 3.12.1 (tags/v3.12.1:2305ca5, Dec  7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
集合分为set(),可变的 和 frozenset(),不可变的对象
t=frozenset("FishC")
t
frozenset({'s', 'F', 'i', 'h', 'C'})   (不可变的集合)
上节课学习的6种方法和6种运算符也使用于不可变的frozenset()函数
六种方法
t.issubset("FishC.com.cn")
True
t.issuperset("Fish")
True
t.union({1,2,3})
frozenset({1, 's', 'F', 2, 'C', 3, 'i', 'h'})
t.intersection("Fish")
frozenset({'s', 'F', 'h', 'i'})
t.difference("Fish")
frozenset({'C'})
上面三个(并集、交集、差集)都支持多参数的
t.union({1,2,3},"Python")
frozenset({'P', 1, 2, 'F', 3, 't', 'i', 'o', 'n', 's', 'y', 'h', 'C'})
t.intersection("Php","Python")
frozenset({'h'})
t.difference("Php","Python")
frozenset({'s', 'F', 'C', 'i'})
第6个对称差集(这个不能支持多参数)
t.symmetric_difference("Python")
frozenset({'P', 's', 'F', 't', 'i', 'o', 'y', 'n', 'C'})

六种运算符
t <= set("FishC")
True
t < set("FishC")
False
t < set("FishC.com.cn")
True
t > set("FishC")
False
t >= set("FishC")
True
t | {1,2,3} | set("Python")
frozenset({'P', 1, 's', 'F', 2, 3, 't', 'i', 'o', 'y', 'h', 'n', 'C'})
t & set("Php") & set("Python")
frozenset({'h'})
t - set("Php") - set("Python")
frozenset({'s', 'F', 'C', 'i'})
t ^ set("Python")
frozenset({'P', 'F', 't', 'i', 'o', 'n', 's', 'y', 'C'})
这节课我们将继续学习那些仅适用于set()对象的方法,会对集合中的内容进行改动的方法。
一、update(*others)
s=set("FishC")
s
{'s', 'F', 'i', 'h', 'C'}
s.update([1,1],"23")     (这种方法是union_update方法的省略)
s
{1, 's', 'F', 'i', '2', '3', 'h', 'C'}       (集合是不可重复的,是无序的)
t
frozenset({'s', 'F', 'i', 'h', 'C'})
t.update([1,1],"23")          (t是一个不可变的集合,所以无法更新,报错)
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    t.update([1,1],"23")
AttributeError: 'frozenset' object has no attribute 'update'
s.intersection_update("FishC")

二、intersection_update(*others) 交集
difference_update(*others)   差集
symmetric_difference_update(*others)  对称差集

有了update,这三种方法会更改集合里面内容的
s
{'s', 'F', 'i', 'h', 'C'}
s.intersection_update("FishC.com.cn")
s
{'s', 'F', 'i', 'h', 'C'}
s.difference_update("Php","Python")
s
{'s', 'F', 'i', 'C'}
s.symmetric_difference_update("Python")
s
{'P', 's', 'F', 't', 'i', 'o', 'y', 'h', 'n', 'C'}
s.union_update("23")  
Traceback (most recent call last):
  File "<pyshell#36>", line 1, in <module>
    s.union_update("23")
AttributeError: 'set' object has no attribute 'union_update'
由于union_update方法使用较多,这里Python直接把union省略,直接命名为update,这个update方法刚刚讲过。报错原因:应该简写。
三、如果单纯想集合里添加数据,可以使用add(elem)的方法。还有删除方法

s.add("23")      (与前面使用update(“23”)的方法对比,传入字符串是迭代获取每一个字符作为元素插入到集合中的,但是add()方法却是传入字符串是将整个字符串作为一个元素插入到集合中的)
s
{'P', 's', 'F', 't', 'i', 'o', 'y', '23', 'h', 'n', 'C'}

s.remove("牛首")
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    s.remove("牛首")
KeyError: '牛首'
s.discard("牛首")
s
{'P', 's', 'F', 't', 'i', 'o', 'y', '23', 'h', 'n', 'C'}
删除还有一个pop()方法随机从集合中弹出一个元素,存入集合的数据是随机的,所以弹出也是随机的。
s.pop()
'P'
s.pop()
's'
s.pop()
'F'
s
{'t', 'i', 'o', 'y', '23', 'h', 'n', 'C'}
s.clear
<built-in method clear of set object at 0x00000224C0410BA0>
s.clear()
s
set()           (清空了)
四‘最后一个非常重要的方法:可哈希
集合和字典有一个刚性需求的,字典的键和集合的元素,它们都是必须是可哈希的;如果一个对象是可哈希的,要求哈希值必须在整个程序的生命周期保持不变。
首先通过hash(object)函数获得一个对象的哈希值。

hash(1)        (整数的哈希值等于本身)
1
hash(1.0)       (如果两个对象的值是相等的,尽管他们是不同对象,1和1.0,一个是整数,一个是浮点数,但是它们的值是相等的,那么他们的哈希值也应该相等的。
1
hash(1.001)         (哈希值对于字典和集合的实现却可以说木之根,水之源,有兴趣可以看一下《字典和集合高效背后的玄机》)
2305843009213441
hash("FishC")             (Python中大多数不可变对象是可哈希的,可变对象是不可哈希的)
6797703855672109279
hash([1,2,3])
Traceback (most recent call last):
  File "<pyshell#53>", line 1, in <module>
    hash([1,2,3])
TypeError: unhashable type: 'list'
hash((1,2,3))
529344067295497451
{"Python":520,"FishC":1314}     (只有可哈希的对象才可作为字典的键,以及集合的元素)
{'Python': 520, 'FishC': 1314}
{[1,2,3]:"FishC"}            (列表是不可哈希的,不能作为集合的键)
Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    {[1,2,3]:"FishC"}
TypeError: unhashable type: 'list'
{"Python","FishC",520,1314}
{520, 'Python', 1314, 'FishC'}
{"Python","FishC",520,1314,[1,2,3]}
Traceback (most recent call last):
  File "<pyshell#58>", line 1, in <module>
    {"Python","FishC",520,1314,[1,2,3]}
TypeError: unhashable type: 'list'
如果要实现一个嵌套的集合,行不行?
x=[1,2,3]
y={x,5,6}
Traceback (most recent call last):
  File "<pyshell#60>", line 1, in <module>
    y={x,5,6}
TypeError: unhashable type: 'list'    (报错,看来嵌套的集合也不行)
那非要实现一个嵌套的集合,有没有办法?
x=frozenset(x)     (把集合变成不可变集合)
y={x,4,5}
y
{frozenset({1, 2, 3}), 4, 5} (魔高一尺,道高一丈;你有张良计,我有过墙梯。)

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:admin@discuz.vip

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鄂ICP备13007585号-4

在本发帖
关注公众号
QQ客服返回顶部