Python学习week4-set集合

1、python数据结构之set

1.1、set集合的定义及初始化

# set特点:set一种是可变的,无序的,去重复的元素集合;set集合可以直接作为字典的key;

class set(object):
    """
    set() -> new empty set object
    set(iterable) -> new set object
Build an unordered collection of unique elements.
"""
例如:
s=set(i for i in range(10) if i %2==0)
print(s,type(s))
'''
{0, 2, 4, 6, 8} <class 'set'>
'''
s1={}   # 定义空字典
s2=set() # 定义空集合

print(s1,s2)
print(type(s1),type(s2))
'''
{} set()
<class 'dict'> <class 'set'>
'''
s3={[1],(1,),1}  # 报错,TypeError: unhashable type: 'list' , 集合是元素类型必须是不可变的数据类型;

# set集合元素:

  • set的元素必须是可hash;
  • set元素不可索引;
  • set集合可迭代;

1.2、set元素的增删改查

1.2.1、set元素的增加

(1)add(elem)

def add(self, *args, **kwargs): # real signature unknown
    """
    Add an element to a set.
        
    This has no effect if the element is already present.
    ""
例如:
s2=set()
s2.add(1)
s2.add(2)
print(s2)
'''
{1, 2}
'''

(2)update(*others)

def update(self, *args, **kwargs): # real signature unknown
    """ Update a set with the union of itself and others. """
    pass
例如:
s3={'a','b'}
s3.update(range(10))
print(s3)
'''
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b'}
'''

1.2.2、set元素的删除

(1)remove(elem)

  • 从set集合中移除一个元素;
  • 元素不存在,抛出keyerror异常 # set可以看出一种特殊的字典;
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.
    """

(2)discard(elem)

  • 从set中移除一个元素;
  • 如果没有该元素,则什么也不做;
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.
    """

(3)pop() ==>item

  • 移除并返回任意的元素;(由于set是无序的,所以pop就是随机删除元素,并将其元素返回)
  • 空集合返回keyerror
def pop(self, *args, **kwargs): # real signature unknown
    """
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.
    """

(4)clear

  • 清除集合内的所有元素;

1.2.3、set元素的修改

(1)set元素的修改?

  • set集合的元素要么删除,要么加入新的元素,没有修改方法;

(2)set元素的查询

  • 由于set为非线性数据结构,无法索引;

(3)set的遍历

  • 可以遍历set集合里面的所有元素;

1.3、set成员运算

# 成员运算符:in和not in

# set非线性结构和list线性结构成员运算的效率比较

  • 线性结构的查询时间复杂度是O(n),即随着数据规模越来越大则效率越来越低;
  • set,dict等非线性结构,内部使用hash值作为key,时间复杂度为O(1);
  • 线性结构尽量少用in或not in来判断一个元素是否在一个容器中;

1.4、set集合运算

1.4.1、集合概念

  • 全集:所有元素的集合;
  • 子集subset和超集superset;
    • 一个集合A所有元素都在另一个集合B内,A是B的子集,B是A的超集;
  • 真子集和真超集
    • A是B的子集,且A不等于B,A就是B的真子集,B是A的真超集;
  • 并集:多个集合合并的结果
  • 交集:多个集合的公共部分
  • 差集:集合中出去和其他集合公共的部分
  • 对称差集:两个集合的对称差是只属于其中一个集合,而不属于另一个集合的元素组成的集合;

1.4.2、集合运算之并集

  • 将两个集合A和B的所有元素合并到一起,组成的集合称为A与B的并集;
  • union(*others)  ==  运算符 ’|‘
    • 返回和多个集合合并后的新的集合;
    •  运算符|,等同于union;
  • update(*others) == 运算符 ’|=’
    • 合并多个集合,就地修改;
s1=set(range(5))
s2=set(range(7,10))

print(s1|s2)
print(s1.union(s2))
print(s1.update(s2)) # 返回None,直接对集合s1进行修改
print('s1:',s1)
print('s2:',s2)
'''
{0, 1, 2, 3, 4, 7, 8, 9}
{0, 1, 2, 3, 4, 7, 8, 9}
None
s1: {0, 1, 2, 3, 4, 7, 8, 9}
s2: {8, 9, 7}
'''

1.4.3、交集

  • intersection(*other) == 运算符 ‘&‘
    • 返回多个集合的交集;
  • intersection_update(*others)   == 运算符 ‘&=’
    • 就地修改;

1.4.4、差集

  • 集合A和B,A对于B的差集为属于A但不属于B的部分为A与B的差集;
  • different(*others) == 运算符 ‘-’
  • different_update(*others) == 运算符'-='

1.4.5、对称差集

  •  集合A和B,由所有不属于A和B交集元素组成,可理解为(A-B)|(B-A);
  • symmetric_difference(*others) == 运算符 '^'

1.4.6、集合运算

  • issbuset(other)与<= :判断当前集合是否是另一个集合的的子集;
  • set1<set2:判断set1是否是set2的真子集;
  • isdisjoint(other):判断当前集合和另一个集合没有交集,没有交集则返回True;
原文地址:https://www.cnblogs.com/soulgou123/p/9494507.html