集合类型— set, frozenset
set 对象是由具有唯一性的hashable 对象所组成的无序多项集。常见的用途包括成员检测、从序列中去除重复项以及数学中的集合类计算,例如交集、并集、差集与对称差集等等
两个类的构造器具有相同的作用方式:
- class set([iterable ])
- class frozenset([iterable ])
集合可用多种方式来创建:
- 使用花括号内以逗号分隔元素的方式: {‘jack’, ‘sjoerd’}
- 使用集合推导式: {c for c in ‘abracadabra’ if c not in ‘abc’}
- 使用类型构造器: set(), set(‘foobar’), set([‘a’, ‘b’, ‘foo’])
set 和frozenset 的实例提供以下操作:
len(s)
计算集合 s 元素个数
x in s
检测x是否为s中的成员
x not in s
检测x 是否非s 中的成员
isdisjoint(other)
用于判断两个集合是否包含相同的元素,如果没有返回 True,否则返回 False
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } y = { "google" , "runoob" , "facebook" } z = x.isdisjoint(y) print (z) |
issubset(other)
用于判断集合的所有元素是否都包含在指定集合中,如果是则返回 True,否则返回 False
1
2
3
|
x = { "a" , "b" , "c" } y = { "f" , "e" , "d" , "c" , "b" , "a" } z = x.issubset(y) |
issuperset(other)
用于判断指定集合的所有元素是否都包含在原始的集合中,如果是则返回 True,否则返回 False。
1
2
3
4
|
x = { "f" , "e" , "d" , "c" , "b" , "a" } y = { "a" , "b" , "c" } z = x.issuperset(y) print (z) |
union(*others)
返回两个集合的并集,即包含了所有集合的元素,重复的元素只会出现一次
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } y = { "google" , "runoob" , "apple" } z = x.union(y) print (z) |
intersection(*others)
用于返回两个或更多集合中都包含的元素,即交集。
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } y = { "google" , "runoob" , "apple" } z = x.intersection(y) print (z) |
difference(*others)
用于返回集合的差集,即返回的集合元素包含在第一个集合中,但不包含在第二个集合(方法的参数)中。
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } y = { "google" , "microsoft" , "apple" } z = x.difference(y) print (z) |
symmetric_difference(other)
返回两个集合中不重复的元素集合,即会移除两个集合中都存在的元素。
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } y = { "google" , "runoob" , "apple" } z = x.symmetric_difference(y) print (z) |
copy()
用于拷贝一个集合。
1
2
3
|
sites = { "Google" , "Runoob" , "Taobao" } x = sites.copy() print (x) |
可用于set 而不能用于不可变的frozenset 实例的操作:
update(*others)
用于修改当前集合,可以添加新的元素或集合到当前集合中,如果添加的元素在集合中已存在,则该元素只会出现一次,重复的会忽略。
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } y = { "google" , "runoob" , "apple" } x.update(y) print (x) |
intersection_update(*others)
- intersection_update() 方法用于获取两个或更多集合中都重叠的元素,即计算交集。
- intersection_update() 方法不同于 intersection() 方法,因为 intersection() 方法是返回一个新的集合,而 intersection_update() 方法是在原始的集合上移除不重叠的元素。
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } # y 集合不包含 banana 和 cherry,被移除 y = { "google" , "runoob" , "apple" } x.intersection_update(y) print (x) |
difference_update(*others)
- difference_update() 方法用于移除两个集合中都存在的元素。
- difference_update() 方法与 difference() 方法的区别在于 difference() 方法返回一个移除相同元素的新集合,而 difference_update() 方法是直接在原来的集合中移除元素,没有返回值。
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } y = { "google" , "microsoft" , "apple" } x.difference_update(y) print (x) |
symmetric_difference_update(other)
symmetric_difference_update() 方法移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中
1
2
3
4
|
x = { "apple" , "banana" , "cherry" } y = { "google" , "runoob" , "apple" } x.symmetric_difference_update(y) print (x) |
add(elem)
用于给集合添加元素,如果添加的元素在集合中已存在,则不执行任何操作。
1
2
3
|
fruits = { "apple" , "banana" , "cherry" } fruits.add( "orange" ) print (fruits) |
remove(elem)
用于移除集合中的指定元素。
1
2
3
|
fruits = { "apple" , "banana" , "cherry" } fruits.remove( "banana" ) print (fruits) |
discard(elem)
如果元素elem 存在于集合中则将其移除
1
2
3
|
fruits = { "apple" , "banana" , "cherry" } fruits.discard( "banana" ) print (fruits) |
pop()
从集合中移除并返回任意一个元素。如果集合为空则会引发KeyError。
1
2
3
|
fruits = { "apple" , "banana" , "cherry" } fruits.pop() print (fruits) |
clear()
用于移除集合中的所有元素。
1
2
3
|
fruits = { "apple" , "banana" , "cherry" } fruits.clear() print (fruits) |
关系运算
1
2
3
4
5
6
7
|
s_1024 = { "佩奇" , "老男孩" , "海峰" , "马JJ" , "老村长" , "黑姑娘" , "Alex" } s_pornhub = { "Alex" , "Egon" , "Rain" , "马JJ" , "Nick" , "Jack" } print (s_1024 & s_pornhub) # 交集, elements in both set print (s_1024 | s_pornhub) # 并集 or 合集 print (s_1024 - s_pornhub) # 差集 , only in 1024 print (s_pornhub - s_1024) # 差集, only in pornhub print (s_1024 ^ s_pornhub) # 对称差集, 把脚踩2只船的人T出去 |
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注服务器之家的更多内容!
原文链接:https://blog.csdn.net/sinat_28317385/article/details/123467456