作者博文地址:https://www.cnblogs.com/liu-shuai/
简介
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联 合), intersection(交), difference(差)和sysmmetric difference(对称差集)等数学运算.
创建集合
1 >>> S1 = set('spiritman')
2 >>> print S1
3 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
集合常用操作及实例展示
可以使用dir(set)查看集合支持的操作方法
add
1 功能:增加一个元素到集合。当集合存在该元素时,该语句不生效
2 Add an element to a set.This has no effect if the element is already present.
3 语法:S.add(object)
4 实例展示:
5 >>> S1 = set('spirit')
6 >>> print S1
7 set(['i', 'p', 's', 'r', 't'])
8 >>> S1.add('a')
9 >>> print S1
10 set(['a', 'i', 'p', 's', 'r', 't'])
11 >>> S1.add('a')
12 >>> print S1
13 set(['a', 'i', 'p', 's', 'r', 't'])
clear
1 功能:清空集合
2 Remove all elements from this set.
3 语法:S.clear()
4 实例展示:
5 >>>S1 = set('spirit')
6 >>> print S1
7 set(['i', 'p', 's', 'r', 't'])
8 >>> S1.clear()
9 >>> print S1
10 set([])
copy
1 功能:浅复制集合,返回一个新的集合。
2 Return a shallow copy of a set.
3 语法:S.copy()
4 实例展示:
5 >>> S1 = set('spirit')
6 >>> print S1
7 set(['i', 'p', 's', 'r', 't'])
8 >>> S2 = S1.copy()
9 >>> print S2
10 set(['i', 'p', 's', 'r', 't'])
11 >>> id(S2)
12 140239642434120
13 >>> print S1
14 set(['i', 'p', 's', 'r', 't'])
15 >>> id(S1)
16 140239644667136
difference
1 功能:找出两个或多个集合中的不同元素,结果返回一个新的集合
2 Return the difference of two or more sets as a new set.(all elements that are in this set but not the others.)
3 语法:S.differencce(set1,set2.....)
4 实例展示:
5 >>> S1 = set('spirita')
6 >>> print S1
7 set(['a', 'i', 'p', 's', 'r', 't'])
8 >>> S2 = set('spiri')
9 >>> print S2
10 set(['i', 'p', 's', 'r'])
11 >>> S3 = set('liush')
12 >>> print S3
13 set(['i', 'h', 's', 'u', 'l'])
14 ##################################################
15 #找出S1和S2中的不同元素,结果返回一个新的集合
16 >>> S1.difference(S2)
17 set(['a', 't'])
18 ##################################################
19 #找出S1和S3中的不同元素,结果返回一个新的集合
20 >>> S1.difference(S3)
21 set(['a', 'p', 'r', 't'])
22 ##################################################
23 #找出S1、S2和S3中的不同元素,结果返回一个新的集合
24 >>> S1.difference(S2,S3)
25 set(['a', 't'])
difference_update
1 功能:删除集合S中所有跟S1中相同的元素。无相同元素时,各个集合不会发生改变。
2 Remove all elements of another set from this set.
3 语法:S.difference_update(S1)
4 实例展示:
5 >>> S1 = set('spirit')
6 >>> print S1
7 set(['i', 'p', 's', 'r', 't'])
8 >>> S2 = set('abcde')
9 >>> print S2
10 set(['a', 'c', 'b', 'e', 'd'])
11 >>> S3 = set('spiritman')
12 >>> print S3
13 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
14 >>> S1.difference_update(S2)
15 >>> print S1
16 set(['i', 'p', 's', 'r', 't'])
17 >>> print S2
18 set(['a', 'c', 'b', 'e', 'd'])
19 >>> S1.difference_update(S3)
20 >>> print S1
21 set([])
22 >>> print S3
23 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
24 >>> S2.difference_update(S3)
25 >>> print S2
26 set(['c', 'b', 'e', 'd'])
27 >>> print S3
28 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
discard
1 功能:从集合中删除一个元素。一次只能删除一个。
2 Remove an element from a set if it is a member.
3 语法:S.discard(object)
4 实例展示:
5 S3 = set('spiritman')
6 >>> print S3
7 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
8 >>> S3.discard('a')
9 >>> print S3
10 set(['i', 'm', 'n', 'p', 's', 'r', 't'])
11 ####################################################
12 #删除多个元素时报错
13 >>>S3.discard('i','m')
14 Traceback (most recent call last):
15 File "<stdin>", line 1, in <module>
16 TypeError: discard() takes exactly one argument (2 given)
intersection
1 功能:求两个或多个集合的交集,交集返回一个新的集合。
2 Return the intersection of two or more sets as a new set.(elements that are common to all of the sets.)
3 语法:S.intersection(set1,set2...)
4 实例展示:
5 >>> S1 = set('spirit')
6 >>> print S1
7 set(['i', 'p', 's', 'r', 't'])
8 >>> S2 = set('spiman')
9 >>> print S2
10 set(['a', 'i', 'm', 'n', 'p', 's'])
11 >>> S3 = set('abcis')
12 >>> print S3
13 set(['a', 'i', 'c', 'b', 's'])
14 ##################################################
15 #求S1和S2的交集
16 >>> S1.intersection(S2)
17 set(['i', 'p', 's'])
18 ##################################################
19 #求S1和S3的交集
20 >>> S1.intersection(S3)
21 set(['i', 's'])
22 ##################################################
23 #求S1、S2和S3的交集
24 >>> S1.intersection(S2,S3)
25 set(['i', 's'])
intersection_update
1 功能: 以一个集合和另一个集合的交集更新该集合。
2 Update a set with the intersection of itself and another.
3 语法:S.intersection_update(set1)
4 实例展示:
5 >>> S1 = set('spirit')
6 >>> print S1
7 set(['i', 'p', 's', 'r', 't'])
8 >>> S2 = set('spiman')
9 >>> print S2
10 set(['a', 'i', 'm', 'n', 'p', 's'])
11 >>> S3 = set('abcis')
12 >>> print S3
13 set(['a', 'i', 'c', 'b', 's'])
14 ##################################################
15 #以S1和S3的交集更新S1
16 >>> S1.intersection_update(S3)
17 >>> print S1
18 set(['i', 's'])
19 ##################################################
20 #以S2和S3的交集更新S2
21 >>> S2.intersection_update(S3)
22 >>> print S2
23 set(['a', 'i', 's'])
isdisjoint
1 功能:判断两个集合是否有集合。若没有则返回True;反之False。
2 Return True if two sets have a null intersection.
3 语法:S.isdisjoint(set1)
4 实例展示:
5 >>> S1 = set('spirit')
6 >>> print S1
7 set(['i', 'p', 's', 'r', 't'])
8 >>> S2 = set('spiman')
9 >>> print S2
10 set(['a', 'i', 'm', 'n', 'p', 's'])
11 >>> S3 = set('abcdef')
12 >>> print S3
13 set(['a', 'c', 'b', 'e', 'd', 'f'])
14 ####################################################
15 >>> S1.isdisjoint(S2)
16 False
17 >>> S1.isdisjoint(S3)
18 True
19 >>> S2.isdisjoint(S3)
20 False
issubset
1 功能:判断集合是否是另一个集合的子集合。是则返回True;反之False.
2 Report whether another set contains this set.
3 语法:S.issubset(set1)
4 实例展示:
5 >>> S1 = set('spirit')
6 >>> print S1
7 set(['i', 'p', 's', 'r', 't'])
8 >>> S2 = set('spiritman')
9 >>> print S2
10 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
11 >>> S3 = set('abcis')
12 >>> print S3
13 set(['a', 'i', 'c', 'b', 's'])
14 ##################################################
15 >>> S1.issubset(S2)
16 True
17 >>> S3.issubset(S2)
18 False
19 >>> S1.issubset(S3)
20 False
issuperset
1 功能:判断集合是否包含另一个集合。
2 Report whether this set contains another set
3 语法:issuperset(set1)
4 实例展示:
5 >>> S1 = set('spiritman')
6 >>> print S1
7 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
8 >>> S2 = set('spirit')
9 >>> print S2
10 set(['i', 'p', 's', 'r', 't'])
11 >>> S1.issuperset(S2)
12 True
pop
1 功能:删除并返回任意一个元素。
2 Remove and return an arbitrary set element.Raises KeyError if the set is empty.
3 语法:S.pop()
4 实例展示:
5 >>> S1 = set('spiritman')
6 >>> print S1
7 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
8
9 >>> S1.pop()
10 'a'
11 >>> S1.pop()
12 'i'
13 >>> S1.pop()
14 'm'
15 >>> print S1
16 set(['n', 'p', 's', 'r', 't'])
remove
1 功能:删除集合中一个指定的元素。
2 Remove an element from a set; it must be a member.If the element is not a member, raise a KeyError
3 语法:S.remove(object)
4 实例展示:
5 >>> S1 = set('spiritman')
6 >>> print S1
7 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
8 >>> S1.remove('n')
9 >>> print S1
10 set(['a', 'i', 'm', 'p', 's', 'r', 't'])
11 >>> S1.remove('b')
12 Traceback (most recent call last):
13 File "<stdin>", line 1, in <module>
14 KeyError: 'b'
symmetric_difference
1 功能:取两个集合的差集,返回一个新集合。
2 Return the symmetric difference of two sets as a new set.(all elements that are in exactly one of the sets.)
3 语法:S.symmetric_difference(set1)
4 实例展示:
5 >>> S1 = set('spiritman')
6 >>> print S1
7 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
8 >>> S2 = set('spirit')
9 >>> print S2
10 set(['i', 'p', 's', 'r', 't'])
11 >>> S3 = set('bcde')
12 >>> print S3
13 set(['c', 'b', 'e', 'd'])
14 ##################################################
15 #返回S1和S2差集
16 >>> S1.symmetric_difference(S2)
17 set(['a', 'm', 'n'])
18 ##################################################
19 #返回S1和S3差集
20 >>> S1.symmetric_difference(S3)
21 set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
22 ##################################################
23 #返回S2和S3差集
24 >>> S2.symmetric_difference(S3)
25 set(['c', 'b', 'e', 'd', 'i', 'p', 's', 'r', 't'])
symmetric_difference_update
1 功能:取集合和另一个集合的差集,更新该集合。
2 Update a set with the symmetric difference of itself and another.
3 语法:symmetric_difference_update(set1)
4 实例展示:
5 >>> S1 = set('spiritman')
6 >>> print S1
7 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
8 >>> S2 = set('spirit')
9 >>> print S2
10 set(['i', 'p', 's', 'r', 't'])
11 >>> S3 = set('bcde')
12 >>> print S3
13 set(['c', 'b', 'e', 'd'])
14 ####################################################
15 >>> S1.symmetric_difference_update(S2)
16 >>> print S1
17 set(['a', 'm', 'n'])
18
19 >>> S2.symmetric_difference_update(S3)
20 >>> print S2
21 set(['c', 'b', 'e', 'd', 'i', 'p', 's', 'r', 't'])
union
1 功能:取多个集合的并集,返回一个新的集合。
Return the union of sets as a new set.(all elements that are in either set.)
2 语法:S.union(set1,set2....)
3 实例展示:
4 >>> S1 = set('spiritman')
5 >>> print S1
6 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
7 >>> S2 = set('spirit')
8 >>> print S2
9 set(['i', 'p', 's', 'r', 't'])
10 >>> S3 = set('bcde')
11 >>> print S3
12 set(['c', 'b', 'e', 'd'])
13 ##################################################
14 #返回S1和S2的并集
15 >>> S1.union(S2)
16 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
17 ##################################################
18 #返回S1和S3的并集
19 >>> S1.union(S3)
20 set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
21 #################################################
22 #返回S1、S2和S3的并集
23 >>> S1.union(S2,S3)
24 set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])
update
1 功能:取一个集合和另一个集合的并集,更新该集合。
2 Update a set with the union of itself and others.
3 语法:S.update(set1)
4 实例展示:
5 >>> S1 = set('spiritman')
6 >>> print S1
7 set(['a', 'i', 'm', 'n', 'p', 's', 'r', 't'])
8 >>> S3 = set('bcde')
9 >>> print S3
10 set(['c', 'b', 'e', 'd'])
11 >>> S1.update(S3)
12 >>> print S1
13 set(['a', 'c', 'b', 'e', 'd', 'i', 'm', 'n', 'p', 's', 'r', 't'])