Python必备技巧之集合Set的使用
目录
定义一个集合集合的大小和成员资格集合的9种操作计算集合并集计算集合交集计算集合之间差异计算集合间对称差计算后集合中是否有包含前集合的元素计算一个集合是否是另一个集合的子集计算一个集合是否是另一个集合的真子集计算一个集合是否是另一个集合的超集计算一个集合是否是另一个集合的正确超集集合的9种修改update计算并集intersection_update 计算交集difference_update 按差异修改被处理集合symmetric_difference_update 按对称差修改被处理集合add 元素添加到集合中remove 集合中移除一个元素discard 集合中移除一个元素pop 集合中移除一个随机元素clear 清空集合被冻结集合在数学中,对集合的严格定义可能是抽象的且难以掌握。但实际上可以将集合简单地认为是定义明确的不同对象的集合,通常称为元素或成员。
Python 提供了一个内置的集合类型来将对象分组到一个集合中。集合与其他对象类型的区别在于可以对执行的独特操作。
定义一个集合
集合是无序的,并且元素是唯一的,集合本身可以修改,但集合中包含的元素必须是不可变类型。
构建集合的方式
# 构建的set数据会自动进行去重 x = set() # list方式 >>> x = set(["foo", "bar", "baz", "foo", "qux"]) >>> x {"qux", "foo", "bar", "baz"} # tuple方式 >>> x = set(("foo", "bar", "baz", "foo", "qux")) >>> x {"qux", "foo", "bar", "baz"} # 字符串方式 >>> s = "quux" >>> list(s) ["q", "u", "u", "x"] >>> set(s) {"x", "u", "q"}
集合元素set后自动排序并且元素必须是不可变的。
>>> x = {42, "foo", (1, 2, 3), 3.14159} >>> x {42, "foo", 3.14159, (1, 2, 3)} # list和dict不能被set >>> a = [1, 2, 3] >>> {a} Traceback (most recent call last): File "", line 1, in {a} TypeError: unhashable type: "list" >>> d = {"a": 1, "b": 2} >>> {d} Traceback (most recent call last): File " ", line 1, in {d} TypeError: unhashable type: "dict"
集合的大小和成员资格
方法 len() 、in 、 not in 的应用。
>>> x = {"foo", "bar", "baz"} >>> len(x) 3 >>> "bar" in x True >>> "qux" in x False
集合的9种操作
计算集合并集
# x1.union(x2[, x3 ...]) # x1 | x2 [| x3 ...] >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"baz", "qux", "quux"} >>> x1 | x2 {"baz", "quux", "qux", "bar", "foo"} >>> x1.union(x2) {"baz", "quux", "qux", "bar", "foo"} # 更多的集合并集操作 >>> a = {1, 2, 3, 4} >>> b = {2, 3, 4, 5} >>> c = {3, 4, 5, 6} >>> d = {4, 5, 6, 7} >>> a.union(b, c, d) {1, 2, 3, 4, 5, 6, 7} >>> a | b | c | d {1, 2, 3, 4, 5, 6, 7}
计算集合交集
# x1.intersection(x2[, x3 ...]) # x1 & x2 [& x3 ...] >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"baz", "qux", "quux"} >>> x1.intersection(x2) {"baz"} >>> x1 & x2 {"baz"} # 更多的集合交集操作 >>> a = {1, 2, 3, 4} >>> b = {2, 3, 4, 5} >>> c = {3, 4, 5, 6} >>> d = {4, 5, 6, 7} >>> a.intersection(b, c, d) {4} >>> a & b & c & d {4}
计算集合之间差异
# x1.difference(x2[, x3 ...]) # x1 - x2 [- x3 ...] >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"baz", "qux", "quux"} >>> x1.difference(x2) {"foo", "bar"} >>> x1 - x2 {"foo", "bar"} # 更多的集合差异操作 >>> a = {1, 2, 3, 30, 300} >>> b = {10, 20, 30, 40} >>> c = {100, 200, 300, 400} >>> a.difference(b, c) {1, 2, 3} >>> a - b - c {1, 2, 3}
计算集合间对称差
# x1.symmetric_difference(x2) # x1 ^ x2 [^ x3 ...] >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"baz", "qux", "quux"} >>> x1.symmetric_difference(x2) {"foo", "qux", "quux", "bar"} >>> x1 ^ x2 {"foo", "qux", "quux", "bar"} # 更多的集合对称差操作 >>> a = {1, 2, 3, 4, 5} >>> b = {10, 2, 3, 4, 50} >>> c = {1, 50, 100} >>> a ^ b ^ c {100, 5, 10}
计算后集合中是否有包含前集合的元素
# x1.isdisjoint(x2) >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"baz", "qux", "quux"} >>> x1.isdisjoint(x2) False >>> x2 - {"baz"} {"quux", "qux"} >>> x1.isdisjoint(x2 - {"baz"}) True # x1.isdisjoint(x2)是True,那么x1 & x2是空集 >>> x1 = {1, 3, 5} >>> x2 = {2, 4, 6} >>> x1.isdisjoint(x2) True >>> x1 & x2 set()
计算一个集合是否是另一个集合的子集
# x1.issubset(x2) # x1 <= x2 >>> x1 = {"foo", "bar", "baz"} >>> x1.issubset({"foo", "bar", "baz", "qux", "quux"}) True >>> x2 = {"baz", "qux", "quux"} >>> x1 <= x2 False # 一个集合被认为是它自身的一个子集 >>> x = {1, 2, 3, 4, 5} >>> x.issubset(x) True >>> x <= x True
计算一个集合是否是另一个集合的真子集
# x1 < x2 >>> x1 = {"foo", "bar"} >>> x2 = {"foo", "bar", "baz"} >>> x1 < x2 True >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"foo", "bar", "baz"} >>> x1 < x2 False # 子集与真子集的判断 >>> x = {1, 2, 3, 4, 5} >>> x <= x True >>> x < x False
计算一个集合是否是另一个集合的超集
# x1.issuperset(x2) # x1 >= x2 >>> x1 = {"foo", "bar", "baz"} >>> x1.issuperset({"foo", "bar"}) True >>> x2 = {"baz", "qux", "quux"} >>> x1 >= x2 False # 集合被认为是本身的一个子集,默认为自身超集 >>> x = {1, 2, 3, 4, 5} >>> x.issuperset(x) True >>> x >= x True
计算一个集合是否是另一个集合的正确超集
# x1 > x2 >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"foo", "bar"} >>> x1 > x2 True >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"foo", "bar", "baz"} >>> x1 > x2 False # 集合不是其自身的正确超集 >>> x = {1, 2, 3, 4, 5} >>> x > x False
集合的9种修改
尽管集合中包含的元素必须是不可变类型,但集合本身可以修改。
update计算并集
# x1.update(x2[, x3 ...]) # x1 |= x2 [| x3 ...] >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"foo", "baz", "qux"} >>> x1 |= x2 >>> x1 {"qux", "foo", "bar", "baz"} >>> x1.update(["corge", "garply"]) >>> x1 {"qux", "corge", "garply", "foo", "bar", "baz"}
intersection_update 计算交集
# x1.intersection_update(x2[, x3 ...]) # x1 &= x2 [& x3 ...] >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"foo", "baz", "qux"} >>> x1 &= x2 >>> x1 {"foo", "baz"} >>> x1.intersection_update(["baz", "qux"]) >>> x1 {"baz"}
difference_update 按差异修改被处理集合
>>> x1 = {"foo", "bar", "baz"} >>> x2 = {"foo", "baz", "qux"} >>> x1 -= x2 >>> x1 {"bar"} >>> x1.difference_update(["foo", "bar", "qux"]) >>> x1 set()
symmetric_difference_update 按对称差修改被处理集合
# x1.symmetric_difference_update(x2) # x1 ^= x2 >>> x1 = {"foo", "bar", "baz"} >>> x2 = {"foo", "baz", "qux"} >>> x1 ^= x2 >>> x1 {"bar", "qux"} >>> >>> x1.symmetric_difference_update(["qux", "corge"]) >>> x1 {"bar", "corge"}
add 元素添加到集合中
>>> x = {"foo", "bar", "baz"} >>> x.add("qux") >>> x {"bar", "baz", "foo", "qux"}
remove 集合中移除一个元素
>>> x = {"foo", "bar", "baz"} >>> x.remove("baz") >>> x {"bar", "foo"} # 如果元素步存在则引发异常 >>> x.remove("qux") Traceback (most recent call last): File "", line 1, in x.remove("qux") KeyError: "qux"
discard 集合中移除一个元素
>>> x = {"foo", "bar", "baz"} >>> x.discard("baz") >>> x {"bar", "foo"} >>> x.discard("qux") >>> x {"bar", "foo"}
pop 集合中移除一个随机元素
>>> x = {"foo", "bar", "baz"} >>> x.pop() "bar" >>> x {"baz", "foo"} >>> x.pop() "baz" >>> x {"foo"} >>> x.pop() "foo" >>> x set() >>> x.pop() Traceback (most recent call last): File "", line 1, in x.pop() KeyError: "pop from an empty set"
clear 清空集合
>>> x = {"foo", "bar", "baz"} >>> x {"foo", "bar", "baz"} >>> >>> x.clear() >>> x set()
被冻结集合
freezeset 为 Python的内置类型,不可变、不可操作。
>>> x = frozenset(["foo", "bar", "baz"]) >>> x frozenset({"foo", "baz", "bar"}) >>> len(x) 3 >>> x & {"baz", "qux", "quux"} frozenset({"baz"})
尝试修改 freezeset 的方法会失败
>>> x = frozenset(["foo", "bar", "baz"]) >>> x.add("qux") Traceback (most recent call last): File "", line 1, in x.add("qux") AttributeError: "frozenset" object has no attribute "add" >>> x.pop() Traceback (most recent call last): File " ", line 1, in x.pop() AttributeError: "frozenset" object has no attribute "pop" >>> x.clear() Traceback (most recent call last): File " ", line 1, in x.clear() AttributeError: "frozenset" object has no attribute "clear" >>> x frozenset({"foo", "bar", "baz"})
以上就是Python必备技巧之集合Set的使用的详细内容,更多关于Python集合Set的资料请关注脚本之家其它相关文章!
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?