python基本数据类型之集合
一、Set(集合)定义
集合(set)是一个无序不重复元素的序列。
基本功能是进行成员关系测试和删除重复元素。
可以使用大括号({})或者 set()函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典。
定义:由不同元素组成的集合,集合中是一组无序排列的可hash值,可以作为字典的key
特性:
1.集合的目的是将不同的值存放到一起,不同的集合间用来做关系运算,无需纠结于集合中单个值
二、集合的创建
s = set()
s = {11,22,33,44}
*注:创建空集合时,只能用set(),如果用第二种方法s={},创建的实际上是一个空字典。
s = {}
print(type(s))
<class 'dict'>
a=set('boy')
b=set(['y', 'b', 'o','o'])
c=set({"k1":'v1','k2':'v2'})
d={'k1','k2','k2'}
e={('k1', 'k2','k2')}
print(a,type(a))
print(b,type(b))
print(c,type(c))
print(d,type(d))
print(e,type(e))
执行结果如下:
{'o', 'b', 'y'} <class 'set'>
{'o', 'b', 'y'} <class 'set'>
{'k1', 'k2'} <class 'set'>
{'k1', 'k2'} <class 'set'>
{('k1', 'k2', 'k2')} <class 'set'>
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. A中存在,B中不存在 (i.e. all elements that are in this set but not the others.) """ pass def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. 从当前集合中删除和B中相同的元素""" pass 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 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 def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. 取交集并更更新到A中 """ pass def isdisjoint(self, *args, **kwargs): # real signature unknown """ Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False""" pass def issubset(self, *args, **kwargs): # real signature unknown """ Report whether another set contains this set. 是否是子序列""" pass def issuperset(self, *args, **kwargs): # real signature unknown """ Report whether this set contains another set. 是否是父序列""" pass def pop(self, *args, **kwargs): # real signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. 移除元素 """ pass 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 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 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ Update a set with the symmetric difference of itself and another. 对称交集,并更新到a中 """ 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 源码
三、基本功能:
- 增加
a=set('python')
a.add('tina')
print(a)
b=set('python')
b.update('tina')
print(b)
执行结果如下:
{'tina', 'o', 'p', 'n', 't', 'y', 'h'}
{'o', 'i', 'p', 'a', 'n', 't', 'y', 'h'}
##################
由以上代码可以看出,add是单个元素的添加,而update是批量的添加。输出结果是无序的,并非添加到尾部。
- 删除(remove,discard,pop)
c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
c.remove('p')
print(c)
c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
c.discard('p')
print(c)
c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
c.pop()
print(c)
执行结果如下: {'i', 'h', 't', 'o', 'y', 'n'}
#####
当执行c.remove('p','i')和c.discard('p','i')时,报错:TypeError: remove() takes exactly one argument (2 given),说明remove和discard删除元素时都只能一个一个的删,同add对应。
#################################################################################
remove,pop和discard的区别:
discard删除指定元素,当指定元素不存在时,不报错;
remove删除指定元素,但当指定元素不存在时,报错:KeyError。
pop删除任意元素,并可将移除的元素赋值给一个变量,不能指定元素移除。
- 清空
c={'p', 'i', 'h', 'n', 'o', 'y', 't'}
c.clear()
print(c)
执行结果如下:
set()
set的特有功能:
s1 = {0}
s2 = {i % 2 for i in range(10)}
s = set('hi')
t = set(['h', 'e', 'l', 'l', 'o'])
print(s.intersection(t), s & t) # 交集
print(s.union(t), s | t) # 并集
print(s.difference(t), s - t) # 差集
print(s.symmetric_difference(t), s ^ t) # 对称差集
print(s1.issubset(s2), s1 <= s2) # 子集(被包含)
print(s1.issuperset(s2), s1 >= s2) # 父集(包含)
执行结果如下:
{'h'} {'h'}
{'i', 'e', 'h', 'l', 'o'} {'i', 'e', 'h', 'l', 'o'}
{'i'} {'i'}
{'e', 'l', 'o', 'i'} {'e', 'l', 'o', 'i'}
True True
False False
s = {11,22,33}
t = {22,44}
print(s.isdisjoint(t))#(disjoint脱节的,)即如果没有交集,返回True,否则返回False
s.difference_update(t)#将差集覆盖到源集合,即从当前集合中删除和B中相同的元素
print(s)
执行结果如下:
False
{33, 11}
s = {11,22,33}
t = {22,44}
s.intersection_update(t)#将交集覆盖到源集合
print(s)
执行结果如下:
{22}
s = {11,22,33}
t = {22,44}
s.symmetric_difference_update(t)#将对称差集覆盖到源集合
print(s)
执行结果如下:
{33, 11, 44}
四、集合的转换
se = set(range(4))
li = list(se)
tu = tuple(se)
st = str(se)
print(li,type(li))
print(tu,type(tu))
print(st,type(st))
执行结果如下:
[0, 1, 2, 3] <class 'list'>
(0, 1, 2, 3) <class 'tuple'>
{0, 1, 2, 3} <class 'str'>

浙公网安备 33010602011771号