python 集合set的创建,更改,遍历,元算合并,交集,补集

2021-01-26 10:30:30 浏览数 (1)

参考链接: Python 集合set clear()

python 集合set的创建,更改,遍历,元算合并,交集,补集 

set的创建,set不允许有重复的元素 

s = set('cheershopa')        可以修改的set,

t = frozenset('bookshopa')   不可须该的set

r = set([1,2,3,34,15,25,35,45,75]) 列表转化到set

t = {} 空set

o = set([]) 空set 

set的联合 交集 差补 

s | t 求s 与    t的并集  结果和s的类型一样

s & t 求 s 与 t的交集,结果是s和类型一样

s - t 求 s 与  t 的差补 结果和s的类型一样

s.union(t)

s.intersection(t)

s.difference(t)

s.copy()

s.superset(t)

s.subset(t)

s |= t

s &= t

s -= t 

set的内建方法help(set) 

>>> help(set) 

Help on class set in module __builtin__:

class set(object)

 |  set() -> new empty set object

 |  set(iterable) -> new set object

 |  

 |  Build an unordered collection of unique elements.

 |  

 |  Methods defined here:

 |  

 |  __and__(...)

 |      x.__and__(y) <==> x&y

 |  

 |  __cmp__(...)

 |      x.__cmp__(y) <==> cmp(x,y)

 |  

 |  __contains__(...)

 |      x.__contains__(y) <==> y in x.

 |  

 |  __eq__(...)

 |      x.__eq__(y) <==> x==y

 |  

 |  __ge__(...)

 |      x.__ge__(y) <==> x>=y

 |  

 |  __getattribute__(...)

 |      x.__getattribute__('name') <==> x.name

 |  

 |  __gt__(...)

 |      x.__gt__(y) <==> x>y

 |  

 |  __iand__(...)

 |      x.__iand__(y) <==> x&=y

 |  

 |  __init__(...)

 |      x.__init__(...) initializes x; see help(type(x)) for signature

 |  

 |  __ior__(...)

 |      x.__ior__(y) <==> x|=y

 |  

 |  __isub__(...)

 |      x.__isub__(y) <==> x-=y

 |  

 |  __iter__(...)

 |      x.__iter__() <==> iter(x)

 |  

 |  __ixor__(...)

 |      x.__ixor__(y) <==> x^=y

 |  

 |  __le__(...)

 |      x.__le__(y) <==> x<=y

 |  

 |  __len__(...)

 |      x.__len__() <==> len(x)

 |  

 |  __lt__(...)

 |      x.__lt__(y) <==> x<y

 |  

 |  __ne__(...)

 |      x.__ne__(y) <==> x!=y

 |  

 |  __or__(...)

 |      x.__or__(y) <==> x|y

 |  

 |  __rand__(...)

 |      x.__rand__(y) <==> y&x

 |  

 |  __reduce__(...)

 |      Return state information for pickling.

 |  

 |  __repr__(...)

 |      x.__repr__() <==> repr(x)

 |  

 |  __ror__(...)

 |      x.__ror__(y) <==> y|x

 |  

 |  __rsub__(...)

 |      x.__rsub__(y) <==> y-x

 |  

 |  __rxor__(...)

 |      x.__rxor__(y) <==> y^x

 |  

 |  __sizeof__(...)

 |      S.__sizeof__() -> size of S in memory, in bytes

 |  

 |  __sub__(...)

 |      x.__sub__(y) <==> x-y

 |  

 |  __xor__(...)

 |      x.__xor__(y) <==> x^y

 |  

 |  add(...)

 |      Add an element to a set.

 |      

 |      This has no effect if the element is already present.

 |  

 |  clear(...)

 |      Remove all elements from this set.

 |  

 |  copy(...)

 |      Return a shallow copy of a set.

 |  

 |  difference(...)

 |      Return the difference of two or more sets as a new set.

 |      

 |      (i.e. all elements that are in this set but not the others.)

 |  

 |  difference_update(...)

 |      Remove all elements of another set from this set.

 |  

 |  discard(...)

 |      Remove an element from a set if it is a member.

 |      

 |      If the element is not a member, do nothing.

 |  

 |  intersection(...)

 |      Return the intersection of two or more sets as a new set.

 |      

 |      (i.e. elements that are common to all of the sets.)

 |  

 |  intersection_update(...)

 |      Update a set with the intersection of itself and another.

 |  

 |  isdisjoint(...)

 |      Return True if two sets have a null intersection.

 |  

 |  issubset(...)

 |      Report whether another set contains this set.

 |  

 |  issuperset(...)

 |      Report whether this set contains another set.

 |  

 |  pop(...)

 |      Remove and return an arbitrary set element.

 |      Raises KeyError if the set is empty.

 |  

 |  remove(...)

 |      Remove an element from a set; it must be a member.

 |      

 |      If the element is not a member, raise a KeyError.

 |  

 |  symmetric_difference(...)

 |      Return the symmetric difference of two sets as a new set.

 |      

 |      (i.e. all elements that are in exactly one of the sets.)

 |  

 |  symmetric_difference_update(...)

 |      Update a set with the symmetric difference of itself and another.

 |  

 |  union(...)

 |      Return the union of sets as a new set.

 |      

 |      (i.e. all elements that are in either set.)

 |  

 |  update(...)

 |      Update a set with the union of itself and others.

 |  

 |  ----------------------------------------------------------------------

 |  Data and other attributes defined here:

 |  

 |  __hash__ = None

 |  

 |  __new__ = <built-in method __new__ of type object>

 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

0 人点赞