python数据类型之集合

python数据类型之集合

数据类型—集合set

特性:无序去重元素必须是不可变类型,集合本身是可变的,可以存所有数据类型的任意多个数据,是可迭代的数据类型

官方帮助文档

class set(object)
 |  set() -> new empty set object
 |  set(iterable) -> new set object
 |  
 |  Build an unordered collection of unique elements.
 |  
 |  Methods defined here:
 |  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.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __hash__ = None

创建集合

class set(object)
| set() -> new empty set object
| set(iterable) -> new set object

迭代创建
>>> set('123')
{'1', '3', '2'}
>>> set(['a','cd'])
{'cd', 'a'}
>>> a = set()
>>> a
set()
直接创建
>>> s = {1,2,3}
>>> s
{1, 2, 3}

集合的方法

['add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']

增加

set1.add(元素)  # 把元素加入集合
>>> set1.add('b')
>>> set1
{'a', 'b'}

set1.update(元素)把元素加到集合中,可以多个值,迭代添加

>>> set1 = {1,2,3,4}
>>> set1.update("cd")
>>> set1
{1, 2, 3,'d', 4, 'c'}

删除

set1.pop() 随机删除,返回的是删除的值,集合为空的时候会报错
>>> set1 = {1,2,3,4}
>>> set1.pop()
1

set1.discard(元素) 没有返回值,没有指定的元素不会报错
>>> set1 = {1,2,3,4}
>>> set1.discard(1)
>>> set1
{2, 3, 4}

>>> set1 = {1,2,3,4}   没有指定元素时不会报错
>>> set1.discard(100)

 
set.remove(元素),没有元素会报错
>>>thisset = set(("Google", "Runoob", "Taobao"))
>>> thisset.remove("Taobao")
>>> print(thisset)
{'Google', 'Runoob'}
>>> thisset.remove("Facebook")   # 不存在会发生错误
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Facebook'

set.clear() 清除集合内的所有元素

求交集(& 或者 intersection)

>>> set1 = {1,2,3,4}
>>> set2 = {3,4,5,6}
>>> set1.intersection(set2)
{3, 4}

>>> set1 & set2
{3, 4}

求并集( | 或者 union)

>>> set1 = {1,2,3,4}
>>> set2 = {3,4,5,6}
>>> set1.union(set2)
{1, 2, 3, 4, 5, 6}

>>> set1 | set2
{1, 2, 3, 4, 5, 6}

求差集 (- 或者 difference)

>>> set1 = {1,2,3,4}
>>> set2 = {3,4,5,6}
>>> set1 - set2      set1独有的
{1, 2}
>>> set2 - set1      set2独有的
{5, 6}

>>> set1 = {1,2,3,4}
>>> set2 = {3,4,5,6}
>>> set1.difference(set2)   返回的时set1中除去去两个集合相同的元素
{1, 2}
>>> set2.difference(set1)   返回的时set2中除去去两个集合相同的元素
{5, 6}

对称差集 除去两个集合相同的元素(^ 或者 symmetric_difference)

>>> set1 = {1,2,3,4}
>>> set2 = {3,4,5,6}
>>> set1 ^ set2
{1, 2, 5, 6}
>>> set1.symmetric_difference(set2)
{1, 2, 5, 6}

判断一个集合是否是另一个集合的子集,子集与超集返回布尔值

set1 = {1,2,3}
set2 = {1,2,3,4,5,6}

print(set1 < set2)  # True
print(set1.issubset(set2))  # 这两个相同,都是说明set1是set2子集。

print(set2 > set1)  # True
print(set2.issuperset(set1))  # 这两个相同,都是说明set2是set1超集。

frozenset

把集合冻住,不能删除,就可以循环

>>> s = frozenset(set1)
>>> print(s,type(s))
frozenset({1, 2, 3}) <class 'frozenset'>
>>> set1.pop()
Traceback (most recent call last):
  File "<pyshell#65>", line 1, in <module>
    set1.pop()
AttributeError: 'frozenset' object has no attribute 'pop'

>>> for i in set1:   # 变成不可变数据类型,可以遍历循环
    print(i)    
2
3
4
原文地址:https://www.cnblogs.com/james201133002/p/9437903.html