前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 集合set的创建,更改,遍历,元算合并,交集,补集

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

作者头像
用户7886150
修改2021-01-26 10:30:30
6610
修改2021-01-26 10:30:30
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: 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

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档