集合 set
集合用于包含一组无序的对象
与列表和元组不同,集合是无序的,也无法通过数字进行索引。
集合中的元素不能重复
set和dict一样,只是没有value,相当于dict的key集合
由于dict的key是不重复的,且key是不可变对象因此set也有如下特性:
▷不重复,(互异性),也就是说集合是天生去重的
▷元素为不可变对象,(确定性,元素必须可hash)
▷集合的元素没有先后之分,(无序性)
python3.x集合的内置函数有17个
class set(object):
"""
set() -> new empty set object
set(iterable) -> new set object
Build an unordered collection of unique elements.
"""
是把要传入的元素做为一个整个添加到集合中
def add(self, *args, **kwargs): # real signature unknown
"""
Add an element to a set.
This has no effect if the element is already present.
"""
pass
清空集合里面的所有元素
def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. """
pass
复制集合里面的所有元素 ,返回一个浅复制
def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. """
pass
求两个集合里面的不同的元素 ,又称差
def difference(self, *args, **kwargs): # real signature unknown
"""
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.)
"""
pass
返回删除了 set “集合2”中含有的元素后的 set “集合1”
def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. """
pass
如果在 set “集合”中存在元素 x, 则删除
def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
"""
pass
求两个集合里面相同的元素,又称并. 返回只保留含有 set “集合2”中元素的 set “集合1”
def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set.
(i.e. all elements that are in both sets.)
"""
pass
返回只保留含有 set “集合2”中元素的 set “集合1” ,并更新自己
def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. """
pass
判断两个集合是不是不相交,并返回
def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. """
pass
判断集合是不是包含其他集合,等同于a>=b
def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. """
pass
判断集合是不是被其他集合包含,等同于a<=b
def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. """
pass
删除并且返回 set “集合”中的一个不确定的元素, 如果为空则引发 KeyError
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass
从 set “集合”中删除元素 , 如果不存在则引发 KeyError
def remove(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
"""
pass
返回一个新的 set 包含外面和里面中不重复的元素,也就是两个集合不重复的元素
def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""
Return the symmetric difference of two sets as a new set.
(i.e. all elements that are in exactly one of the sets.)
"""
pass
返回含有 set “里面”或者 set “外面”中有而不是两者都有的元素的 set “外面”
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the symmetric difference of itself and another. """
pass
把两个集合连接起来,又称并
def union(self, *args, **kwargs): # real signature unknown
"""
Return the union of sets as a new set.
(i.e. all elements that are in either set.)
"""
pass
把两个集合连接起来,又称并
def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
pass
1.定义集合
a1=[1,2,3,4]
print(set(a1))
>>> {1, 2, 3, 4}
#或者直接定义一个集合
a2_set={1,2,3,4,5}
2.增(更新)
set.add()
set.update([])
# set.add() 只能增加一个, 不能同时增加多个值
a1={1,2,3,4}
a1.add(8)
a1
>>> {1, 2, 3, 4, 8}
--------------------------------------
a1.add(11,12) # 同时增加多个值报错
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: add() takes exactly one argument (2 given)
--------------------------------------
""" Update a set with the union of itself and others. """
# a2 集合与 a1 集合 合并更新成为新的 a2 集合
a1={1,2,3,4}
a2={11,12,13}
a2.update(a1)
a2
>>> {1, 2, 3, 4, 8, 11, 12, 13}