python集合操作

python集合操作

一:python官方文档

  https://docs.python.org/3.6/library/stdtypes.html#set-types-set-frozenset

二:集合概念

  • 集合是无序的
  • 集合是无重复元素的

python集合操作

一:add()

会改变原有集合

 >>> s = set('HackerRank')
>>> s.add('H')
>>> print s
set(['a', 'c', 'e', 'H', 'k', 'n', 'r', 'R'])
>>> print s.add('HackerRank')#添加后返回none
None
>>> print s
set(['a', 'c', 'e', 'HackerRank', 'H', 'k', 'n', 'r', 'R'])

set.add()

二:pop(),discard(),remove()

  •  pop()是随机删除元素
  •  discard(),remove()是删除指定元素的
  • discard()在删除不存在元素是不会报错,而remvoe()会报keyerror错误
  • 都会改变原有集合

set.pop()

 >>> s = set([])
>>> print s.pop() >>> print s
set([])
>>> print s.pop()
KeyError: pop from an empty set

set.pop()

set.remove()

 >>> s = set([, , , , , , , , ])
>>> s.remove()
>>> print s
set([, , , , , , , ])
>>> print s.remove()#默认返回None
None
>>> print s
set([, , , , , , ])
>>> s.remove()
KeyError:

set.remove()

set.discard()

 >>> s = set([, , , , , , , , ])
>>> s.discard()
>>> print s
set([, , , , , , , ])
>>> print s.discard()
None
>>> print s
set([, , , , , , ])
>>> s.discard()
>>> print s
set([, , , , , , ])

set.discard()

三:intersetcion(),difference(),unoin(),symmetric_difference(),交集,差集,并集,对称差集

不改变原有集合

set.intersection()

 >>> s = set("Hacker")
>>> print s.intersection("Rank")
set(['a', 'k']) >>> print s.intersection(['R', 'a', 'n', 'k'])
set(['a', 'k']) >>> print s.intersection(enumerate(['R', 'a', 'n', 'k']))
set([]) >>> print s.intersection({"Rank":})
set([]) >>> s & set("Rank")
set(['a', 'k'])

set.intersection()

set.difference()

 #set1有的而set2没有的元素
>>> s = set("Hacker")
>>> print s.difference("Rank")
set(['c', 'r', 'e', 'H']) >>> print s.difference(set(['R', 'a', 'n', 'k']))
set(['c', 'r', 'e', 'H']) >>> print s.difference(['R', 'a', 'n', 'k'])
set(['c', 'r', 'e', 'H']) >>> print s.difference(enumerate(['R', 'a', 'n', 'k']))
set(['a', 'c', 'r', 'e', 'H', 'k']) >>> print s.difference({"Rank":})
set(['a', 'c', 'e', 'H', 'k', 'r']) >>> s - set("Rank")
set(['H', 'c', 'r', 'e'])

set.difference()

set.unoin()

 >>> s = set("Hacker")
>>> print s.union("Rank")
set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n']) >>> print s.union(set(['R', 'a', 'n', 'k']))
set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n']) >>> print s.union(['R', 'a', 'n', 'k'])
set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n']) >>> print s.union(enumerate(['R', 'a', 'n', 'k']))
set(['a', 'c', 'r', 'e', (, 'a'), (, 'n'), 'H', 'k', (, 'k'), (, 'R')]) >>> print s.union({"Rank":})
set(['a', 'c', 'r', 'e', 'H', 'k', 'Rank']) >>> s | set("Rank")
set(['a', 'R', 'c', 'r', 'e', 'H', 'k', 'n'])

set.union()

 set.symmetric_difference()

 >>> s = set("Hacker")
>>> print s.symmetric_difference("Rank")
set(['c', 'e', 'H', 'n', 'R', 'r']) >>> print s.symmetric_difference(set(['R', 'a', 'n', 'k']))
set(['c', 'e', 'H', 'n', 'R', 'r']) >>> print s.symmetric_difference(['R', 'a', 'n', 'k'])
set(['c', 'e', 'H', 'n', 'R', 'r']) >>> print s.symmetric_difference(enumerate(['R', 'a', 'n', 'k']))
set(['a', 'c', 'e', 'H', (, 'R'), 'r', (, 'n'), 'k', (, 'a'), (, 'k')]) >>> print s.symmetric_difference({"Rank":})
set(['a', 'c', 'e', 'H', 'k', 'Rank', 'r']) >>> s ^ set("Rank")
set(['c', 'e', 'H', 'n', 'R', 'r'])

set.symmertic_difference

四:update(),difference_update(),set.symmetric_difference_update()

set.update(set2):以集合2还更新集合1,加入到集合1中

 >> myset.update([, , , ]) # update() only works for iterable objects
>> myset
{'a', , 'c', 'b', , , (, ), }
>> myset.update({, , })
>> myset
{'a', , 'c', 'b', , , , , (, ), }
>> myset.update({, }, [, ])
>> myset
{'a', , 'c', 'b', , , , , , , (, ), , }

set.update()

set.difference_update(set2):用set1-set2来更新set1,覆盖set1值

 #用A,B的差集来更新A
>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.difference_update(R)
>>> print H
set(['c', 'e', 'H', 'r'])

set.difference——update()

set.symmetric_difference_update(set2):用set1和set2的对称差集来更新set1,覆盖set1的值

 #用A,b互相差集来更新A
>>> H = set("Hacker")
>>> R = set("Rank")
>>> H.symmetric_difference_update(R)
>>> print H
set(['c', 'e', 'H', 'n', 'r', 'R'])

set.systemeratic_update()

五:isdisjoint(), issubset(), issuperset()

set.isdisjoint():如果两个集合没有交集则返回True。

s1,s2={,,},{,,}s1.isdisjoint(s2)True

set.isdisjoint()

issubset():是否为子集

issubset(other)
set <= other

Test whether every element in the set is in other.

set < other:严格子集

Test whether the set is a proper subset of other, that is, set <= other and set != other

 >>> set1={,,,,}
>>> set2={,,,,,}
>>> set1.issubset(set2)
True
>>> set3={,,,,,}
>>> set3.issubset(set2)
True
>>> set3<set2
False
>>> set1<set2
True
issuperset(other)
set >= other

Test whether every element in other is in the set.

set > other:严格父集

Test whether the set is a proper superset of other, that is, set >= other and set != other.

https://www.hackerrank.com/challenges/py-check-strict-superset/problem
解决方法
a = set(input().split())
print(all(a > set(input().split()) for _ in range(int(input()))))

  

上一篇:SQL Server XML Path[转]


下一篇:OO第二阶段纪实