Python set 集合

集合

  • 跟数学中集合的概念一致
  • 内容无序 + 内容不重复
# 集合的定义
# 1. 通过set关键字
sa = set()
print(sa)

li =[1,2,3,4,5,4,3,2,12,32,3,2]
sb = set(li)
print(sb)

# 2.使用大括号
sc = {1,2,3,4,1,2,3,43,4,54,12,11}
print(sc)
set()
{32, 1, 2, 3, 4, 5, 12}
{1, 2, 3, 4, 43, 12, 11, 54}
# in 操作
if 2 in sc:
    print(2222222)

if 23 in sc:
    print(nonononono)
    
for i in sc:
    print(i)
2222222
1
2
3
4
43
12
11
54
# 集合的另一种遍历

sa = {(1,2,3),(4,5,6),("i","love","wangxiaojing")}
for i,j,k in sa:
    print(i,j,k)
i love wangxiaojing
4 5 6
1 2 3
# 集合的生成式
sa = {1,2,3,4,5,6,7,8,9,10}
# 利用sa生成一个sb
sb = {i for i in sa}
print(sb)

sc = {i for i in sa if i % 2 == 0}
print(sc)

# 双重for循环
# 把sa中的每一个元素的平方生成一个新的集合
# 1. 用一个for
sd = {i**2 for i in sa}
print(sd)

# 2. 使用两个for
se = { m*n for m in sa for n in sa}
print(se)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{2, 4, 6, 8, 10}
{64, 1, 4, 36, 100, 9, 16, 49, 81, 25}
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, 28, 30, 32, 35, 36, 40, 42, 45, 48, 49, 50, 54, 56, 60, 63, 64, 70, 72, 80, 81, 90, 100}
# 集合的内置函数
# len: 长度
print(len(se))
# max/min:最值
# add: 向集合中添加元素

sa = {1,2,3,4,5,6,5,4,3,2,1}
print(sa)
# 打印结果为什么不是sa,为什么是空
print(sa.add(7))
print(sa)

# clear: 清空
42
{1, 2, 3, 4, 5, 6}
None
{1, 2, 3, 4, 5, 6, 7}
# 删除操作
# remove 和 discard的区别
sa = {1,2,3,4,5,6,7}
print(sa)
sa.remove(5)
print(sa)
# remove删除的值如果不在集合中,报错
sa.remove(5)
print(sa)
{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 6, 7}



---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-19-ea71bdaeb1cc> in <module>()
      6 print(sa)
      7 # remove删除的值如果不在集合中,报错
----> 8 sa.remove(5)
      9 print(sa)


KeyError: 5
sa = {1,2,3,4,5,6,7}
print(sa)
sa.discard(5)
print(sa)
sa.discard(5)
print(sa)
{1, 2, 3, 4, 5, 6, 7}
{1, 2, 3, 4, 6, 7}
{1, 2, 3, 4, 6, 7}
# pop 弹出集合的一个内容
# 删除的内容是随机的
# 删除的内容没啥规律,随机
sa = {1,2,3,4,5,6,7}
print(sa)
sa.pop()
print(sa)
{1, 2, 3, 4, 5, 6, 7}
{2, 3, 4, 5, 6, 7}
# 集合的数学操作
# intersection: 交集
sa = {1,2,3,4,5,6}
sb = {4,5,6,7,8,9}
# sa 和sb的交集
print(sa.intersection(sb))

# difference: 差集
print(sa.difference(sb))
# 差集的另一种表示
print(sa - sb)

# union: 并集
print(sa.union(sb))
print(sa + sb)
{4, 5, 6}
{1, 2, 3}
{1, 2, 3}
{1, 2, 3, 4, 5, 6, 7, 8, 9}



---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-29-b80ff43820df> in <module>()
     13 # union: 并集
     14 print(sa.union(sb))
---> 15 print(sa + sb)


TypeError: unsupported operand type(s) for +: 'set' and 'set'

frzenset冰冻集合

  • 不允许修改的集合
# 案例
print(sa)
sb = frozenset(sa)
print(sb)
{1, 2, 3, 4, 5, 6}
frozenset({1, 2, 3, 4, 5, 6})
help(set)
Help on class set in module builtins:

class set(object)
 |  set() -> new empty set object
 |  set(iterable) -> new set object
 |  
 |  Build an unordered collection of unique elements.
 |  
 |  Methods defined here:
 |  
 |  __and__(self, value, /)
 |      Return self&value.
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x.
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __iand__(self, value, /)
 |      Return self&=value.
 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __ior__(self, value, /)
 |      Return self|=value.
 |  
 |  __isub__(self, value, /)
 |      Return self-=value.
 |  
 |  __iter__(self, /)
 |      Implement iter(self).
 |  
 |  __ixor__(self, value, /)
 |      Return self^=value.
 |  
 |  __le__(self, value, /)
 |      Return self<=value.
 |  
 |  __len__(self, /)
 |      Return len(self).
 |  
 |  __lt__(self, value, /)
 |      Return self<value.
 |  
 |  __ne__(self, value, /)
 |      Return self!=value.
 |  
 |  __or__(self, value, /)
 |      Return self|value.
 |  
 |  __rand__(self, value, /)
 |      Return value&self.
 |  
 |  __reduce__(...)
 |      Return state information for pickling.
 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  __ror__(self, value, /)
 |      Return value|self.
 |  
 |  __rsub__(self, value, /)
 |      Return value-self.
 |  
 |  __rxor__(self, value, /)
 |      Return value^self.
 |  
 |  __sizeof__(...)
 |      S.__sizeof__() -> size of S in memory, in bytes
 |  
 |  __sub__(self, value, /)
 |      Return self-value.
 |  
 |  __xor__(self, value, /)
 |      Return self^value.
 |  
 |  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 sets as a new set.
 |      
 |      (i.e. all elements that are in both 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.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods defined here:
 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None
posted @ 2021-11-25 18:47  野哥李  阅读(9)  评论(0编辑  收藏  举报  来源