Python基本数据类型之set
一、定义
set是一个无序且不重复的元素集合。
集合对象是一组无序排列的可哈希的值,集合成员可以做字典中的键。集合支持用in和not in操作符检查成员,由len()内建函数得到集合的基数(大小), 用 for 循环迭代集合的成员。但是因为集合本身是无序的,不可以为集合创建索引或执行切片(slice)操作,也没有键(keys)可用来获取集合中元素的值。
set和dict一样,只是没有value,相当于dict的key集合,由于dict的key是不重复的,且key是不可变对象因此set也有如下特性:
不重复
元素为不可变对象
二、创建
s = set()
s = {11,22,33,44}
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)) OUTPUT: {'o', 'b', 'y'} <class 'set'> {'o', 'b', 'y'} <class 'set'> {'k1', 'k2'} <class 'set'> {'k1', 'k2'} <class 'set'> {('k1', 'k2', 'k2')} <class 'set'>
三、基本操作
比较
se = {11, 22, 33}
be = {22, 55}
temp1 = se.difference(be)
删除
discard()、remove()、pop()
se = {11, 22, 33}
se.discard(11)
se.discard(44)
取交集
se = {11, 22, 33}
be = {22, 55}
temp1 = se.intersection(be)
判断
se = {11, 22, 33}
be = {22}
print(se.isdisjoint(be))
合并
se = {11, 22, 33}
be = {22}
temp1 = se.symmetric_difference(be)
取并集
se = {11, 22, 33}
be = {22,44,55}
temp=se.union(be)
更新
se = {11, 22, 33}
be = {22,44,55}
se.update(be)
集合的转换
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)) OUTPUT: [0, 1, 2, 3] <class 'list'> (0, 1, 2, 3) <class 'tuple'> {0, 1, 2, 3} <class 'str'>
四、源码
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): """添加""" """ Add an element to a set. This has no effect if the element is already present. """ pass def clear(self, *args, **kwargs): """清除""" """ Remove all elements from this set. """ pass def copy(self, *args, **kwargs): """浅拷贝""" """ Return a shallow copy of a set. """ pass def difference(self, *args, **kwargs): """比较""" """ 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 def difference_update(self, *args, **kwargs): """ Remove all elements of another set from this set. """ pass def discard(self, *args, **kwargs): """删除""" """ 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): """ 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): """ Update a set with the intersection of itself and another. """ pass def isdisjoint(self, *args, **kwargs): """ Return True if two sets have a null intersection. """ pass def issubset(self, *args, **kwargs): """ Report whether another set contains this set. """ pass def issuperset(self, *args, **kwargs): """ Report whether this set contains another set. """ pass def pop(self, *args, **kwargs): """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. """ pass def remove(self, *args, **kwargs): """ 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): """ 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): """ Update a set with the symmetric difference of itself and another. """ pass def union(self, *args, **kwargs): """ Return the union of sets as a new set. (i.e. all elements that are in either set.) """ pass def update(self, *args, **kwargs): """ Update a set with the union of itself and others. """ pass def __and__(self, *args, **kwargs): """ Return self&value. """ pass def __contains__(self, y): """ x.__contains__(y) <==> y in x. """ pass def __eq__(self, *args, **kwargs): """ Return self==value. """ pass def __getattribute__(self, *args, **kwargs): """ Return getattr(self, name). """ pass def __ge__(self, *args, **kwargs): """ Return self>=value. """ pass def __gt__(self, *args, **kwargs): """ Return self>value. """ pass def __iand__(self, *args, **kwargs): """ Return self&=value. """ pass def __init__(self, seq=()):
转载于:https://www.cnblogs.com/xwqhl/p/10690903.html