一:python官方文档
https://docs.python.org/3.6/library/stdtypes.html#set-types-set-frozenset
二:集合概念
- 集合是无序的
- 集合是无重复元素的
一:add()
会改变原有集合
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >>> s = set('HackerRank') 2 >>> s.add('H') 3 >>> print s 4 set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R']) 5 >>> print s.add('HackerRank')#添加后返回none 6 None 7 >>> print s 8 set(['a', 'c', 'e', 'HackerRank', 'H', 'k', 'n', 'r', 'R'])
二:pop(),discard(),remove()
- pop()是随机删除元素
- discard(),remove()是删除指定元素的
- discard()在删除不存在元素是不会报错,而remvoe()会报keyerror错误
- 都会改变原有集合
set.pop()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >>> s = set([1]) 2 >>> print s.pop() 3 1 4 >>> print s 5 set([]) 6 >>> print s.pop() 7 KeyError: pop from an empty set
set.remove()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >>> s = set([1, 2, 3, 4, 5, 6, 7, 8, 9]) 2 >>> s.remove(5) 3 >>> print s 4 set([1, 2, 3, 4, 6, 7, 8, 9]) 5 >>> print s.remove(4)#默认返回None 6 None 7 >>> print s 8 set([1, 2, 3, 6, 7, 8, 9]) 9 >>> s.remove(0) 10 KeyError: 0
set.discard()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >>> s = set([1, 2, 3, 4, 5, 6, 7, 8, 9]) 2 >>> s.discard(5) 3 >>> print s 4 set([1, 2, 3, 4, 6, 7, 8, 9]) 5 >>> print s.discard(4) 6 None 7 >>> print s 8 set([1, 2, 3, 6, 7, 8, 9]) 9 >>> s.discard(0) 10 >>> print s 11 set([1, 2, 3, 6, 7, 8, 9])
三:intersetcion(),difference(),unoin(),symmetric_difference(),交集,差集,并集,对称差集
不改变原有集合
set.intersection()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >>> s = set("Hacker") 2 >>> print s.intersection("Rank") 3 set(['a', 'k']) 4 5 6 >>> print s.intersection(['R', 'a', 'n', 'k']) 7 set(['a', 'k']) 8 9 >>> print s.intersection(enumerate(['R', 'a', 'n', 'k'])) 10 set([]) 11 12 >>> print s.intersection({"Rank":1}) 13 set([]) 14 15 >>> s & set("Rank") 16 set(['a', 'k'])
set.difference()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #set1有的而set2没有的元素 2 >>> s = set("Hacker") 3 >>> print s.difference("Rank") 4 set(['c', 'r', 'e', 'H']) 5 6 >>> print s.difference(set(['R', 'a', 'n', 'k'])) 7 set(['c', 'r', 'e', 'H']) 8 9 >>> print s.difference(['R', 'a', 'n', 'k']) 10 set(['c', 'r', 'e', 'H']) 11 12 >>> print s.difference(enumerate(['R', 'a', 'n', 'k'])) 13 set(['a', 'c', 'r', 'e', 'H', 'k']) 14 15 >>> print s.difference({"Rank":1}) 16 set(['a', 'c', 'e', 'H', 'k', 'r']) 17 18 >>> s - set("Rank") 19 set(['H', 'c', 'r', 'e'])
set.unoin()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >>> s = set("Hacker") 2 >>> print s.union("Rank") 3 set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n']) 4 5 >>> print s.union(set(['R', 'a', 'n', 'k'])) 6 set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n']) 7 8 >>> print s.union(['R', 'a', 'n', 'k']) 9 set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n']) 10 11 >>> print s.union(enumerate(['R', 'a', 'n', 'k'])) 12 set(['a', 'c', 'r', 'e', (1, 'a'), (2, 'n'), 'H', 'k', (3, 'k'), (0, 'R')]) 13 14 >>> print s.union({"Rank":1}) 15 set(['a', 'c', 'r', 'e', 'H', 'k', 'Rank']) 16 17 >>> s | set("Rank") 18 set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n'])
set.symmetric_difference()
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >>> s = set("Hacker") 2 >>> print s.symmetric_difference("Rank") 3 set(['c', 'e', 'H', 'n', 'R', 'r']) 4 5 >>> print s.symmetric_difference(set(['R', 'a', 'n', 'k'])) 6 set(['c', 'e', 'H', 'n', 'R', 'r']) 7 8 >>> print s.symmetric_difference(['R', 'a', 'n', 'k']) 9 set(['c', 'e', 'H', 'n', 'R', 'r']) 10 11 >>> print s.symmetric_difference(enumerate(['R', 'a', 'n', 'k'])) 12 set(['a', 'c', 'e', 'H', (0, 'R'), 'r', (2, 'n'), 'k', (1, 'a'), (3, 'k')]) 13 14 >>> print s.symmetric_difference({"Rank":1}) 15 set(['a', 'c', 'e', 'H', 'k', 'Rank', 'r']) 16 17 >>> s ^ set("Rank") 18 set(['c', 'e', 'H', 'n', 'R', 'r'])
四:update(),difference_update(),set.symmetric_difference_update()
set.update(set2):以集合2还更新集合1,加入到集合1中
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >> myset.update([1, 2, 3, 4]) # update() only works for iterable objects 2 >> myset 3 {'a', 1, 'c', 'b', 4, 2, (5, 4), 3} 4 >> myset.update({1, 7, 8}) 5 >> myset 6 {'a', 1, 'c', 'b', 4, 7, 8, 2, (5, 4), 3} 7 >> myset.update({1, 6}, [5, 13]) 8 >> myset 9 {'a', 1, 'c', 'b', 4, 5, 6, 7, 8, 2, (5, 4), 13, 3}
set.difference_update(set2):用set1-set2来更新set1,覆盖set1值
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #用A,B的差集来更新A 2 >>> H = set("Hacker") 3 >>> R = set("Rank") 4 >>> H.difference_update(R) 5 >>> print H 6 set(['c', 'e', 'H', 'r'])
set.symmetric_difference_update(set2):用set1和set2的对称差集来更新set1,覆盖set1的值
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #用A,b互相差集来更新A 2 >>> H = set("Hacker") 3 >>> R = set("Rank") 4 >>> H.symmetric_difference_update(R) 5 >>> print H 6 set(['c', 'e', 'H', 'n', 'r', 'R'])
五:isdisjoint(), issubset(), issuperset()
set.isdisjoint():如果两个集合没有交集则返回True。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1s1,s2={1,2,3},{4,5,6}23s1.isdisjoint(s2)4True
issubset():是否为子集
-
Test whether every element in the set is in other.
issubset
(other)¶
set <= other
-
Test whether the set is a proper subset of other, that is,
set <= other and set != other
set < other:严格子集
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 >>> set1={1,2,3,4,5} 2 >>> set2={1,2,3,4,5,6} 3 >>> set1.issubset(set2) 4 True 5 >>> set3={1,2,3,4,5,6} 6 >>> set3.issubset(set2) 7 True 8 >>> set3<set2 9 False 10 >>> set1<set2 11 True
-
Test whether every element in other is in the set.
issuperset
(other)¶
set >= other
-
Test whether the set is a proper superset of other, that is,
set >= other and set != other
.
set > other:严格父集
a = set(input().split())
print(all(a > set(input().split()) for _ in range(int(input()))))