【python之路11】集合数据类型(set)

python

集合数据类型(set):集合是不重复的无需序列

1、集合数据类型的创建

python;gutter:true;">a = {11,22,33}

#或

a = set() #创建空集合,不能用a={},这样创建的是字典类型

2、集合转换(将可迭代的转换为集合)

li = [11,22,33,11]

se = set(li)

print(se) #返回{33, 11, 22}

3、set方法总结se1 = {11,22}se2 = {22,33,44}se3 = {22,33}

se1.add(33)  #se1返回{33, 11, 22},增加元素

se1.clear() #se1返回set(),清空所有元素

se_new = se1.copy() #新集合se_new 返回{11, 22},浅拷贝

se_new = se1.difference(se2) #返回新集合,se1中存在但se2中不存在的元素

se1.difference_update(se2) #se1更新为se1中存在且se2中不存在的元素,se1={11}

se_new = se1.symmetric_difference(se2) #对称不同,实际返回两个集合并集去掉两个集合中交集元素,se_new={33, 11, 44},

实际上是se1中有se2中没有 并集于 se2中有se1中没有的

se1.symmetric_difference_update(se2) #更新对称不同,se1更新为两个集合并集去掉两个集合中交集元素,se1={33, 11, 44},

实际上是se1中有se2中没有 并集于 se2中有se1中没有的

se_new = se1.intersection(se2) #返回一个新的集合,se_new为两个集合的交集,{22}

se1.intersection_update(se2) #更新se1为se1和se2的交集,se1={22}

se_new = se1.union(se2) #返回新集合,se1与se2的并集

se1.update(se2) #se1更新为,se1与se3的并集,se1={33, 11, 44, 22}

se1.discard(11) #se1={22},抛弃一个元素,如果参数中的元素不存在,不报错

element = se1.pop() #删除任意一个元素,并将删除的元素赋值为element,element=11,se1={22},若果是空集合则抛KeyError异常

se1.remove(11) #从se1中移除11元素,se1 = {22},如果参数在set中不存在这个元素则抛出KeyError异常

bol = se1.isdisjoint(se1) #判断se1与se2是否没有交集,如果没有返回True,如果有返回False,bol=False

bol = se3.issubset(se2) #判断se3是否是子集合,se3的元素完全包含在se2内,则返回True

bol = se2.issuperset(se3) #判断se2是否是父集合,se2的元素完全包含se3,则返回True

4、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.

(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.

从这个集合中删除另一个集合的所有元素"""

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.

更新一个集合用自己和另一个集合的交集"""

pass

def isdisjoint(self, *args, **kwargs): # real signature unknown

""" Return True if two sets have a null intersection.

返回True 如果两个集合没有交集"""

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.

移除并返回任意集合元素

抛出KeyError 如果集合是空的

"""

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.

从集合中移除一个元素;它必须是一个成员。

如果这个元素不是一个成员,抛出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.)

作为一个新的集合返回两个集合的对称不同

(也就是所有元素正好在众集合中的一个)

实际上是a集合存在且b集合中不存在的元素,并集于,b集合存在且a集合不存在的元素

"""

pass

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

def __and__(self, *args, **kwargs): # real signature unknown

""" Return self&value. """

pass

def __contains__(self, y): # real signature unknown; restored from __doc__

""" x.__contains__(y) <==> y in x. """

pass

def __eq__(self, *args, **kwargs): # real signature unknown

""" Return self==value. """

pass

def __getattribute__(self, *args, **kwargs): # real signature unknown

""" Return getattr(self, name). """

pass

def __ge__(self, *args, **kwargs): # real signature unknown

""" Return self>=value. """

pass

def __gt__(self, *args, **kwargs): # real signature unknown

""" Return self>value. """

pass

def __iand__(self, *args, **kwargs): # real signature unknown

""" Return self&=value. """

pass

def __init__(self, seq=()): # known special case of set.__init__

"""

set() -> new empty set object

set(iterable) -> new set object

Build an unordered collection of unique elements.

# (copied from class doc)

"""

pass

def __ior__(self, *args, **kwargs): # real signature unknown

""" Return self|=value. """

pass

def __isub__(self, *args, **kwargs): # real signature unknown

""" Return self-=value. """

pass

def __iter__(self, *args, **kwargs): # real signature unknown

""" Implement iter(self). """

pass

def __ixor__(self, *args, **kwargs): # real signature unknown

""" Return self^=value. """

pass

def __len__(self, *args, **kwargs): # real signature unknown

""" Return len(self). """

pass

def __le__(self, *args, **kwargs): # real signature unknown

""" Return self<=value. """

pass

def __lt__(self, *args, **kwargs): # real signature unknown

""" Return self<value. """

pass

@staticmethod # known case of __new__

def __new__(*args, **kwargs): # real signature unknown

""" Create and return a new object. See help(type) for accurate signature. """

pass

def __ne__(self, *args, **kwargs): # real signature unknown

""" Return self!=value. """

pass

def __or__(self, *args, **kwargs): # real signature unknown

""" Return self|value. """

pass

def __rand__(self, *args, **kwargs): # real signature unknown

""" Return value&self. """

pass

def __reduce__(self, *args, **kwargs): # real signature unknown

""" Return state information for pickling. """

pass

def __repr__(self, *args, **kwargs): # real signature unknown

""" Return repr(self). """

pass

def __ror__(self, *args, **kwargs): # real signature unknown

""" Return value|self. """

pass

def __rsub__(self, *args, **kwargs): # real signature unknown

""" Return value-self. """

pass

def __rxor__(self, *args, **kwargs): # real signature unknown

""" Return value^self. """

pass

def __sizeof__(self): # real signature unknown; restored from __doc__

""" S.__sizeof__() -> size of S in memory, in bytes """

pass

def __sub__(self, *args, **kwargs): # real signature unknown

""" Return self-value. """

pass

def __xor__(self, *args, **kwargs): # real signature unknown

""" Return self^value. """

pass

__hash__ = None

5、实例需求

old_dic = {"#1":11,"#2":22,"#3":33}
new_dic = {"#3":44,"#4":55,"#5":66}
需求:
1、old_dic与new_dic如果字典key值相同,则new的值更新到old里面
2、new里面存在old里面不存在,则在old里面添加
3、old中存在new中不存在则在old中删除

old_dic = {"#1":11,"#2":22,"#3":33}

new_dic = {"#3":44,"#4":55,"#5":66}

old_set = set(old_dic.keys()) #key值转换为集合

new_set = set(new_dic.keys())

set_inter = old_set.intersection(new_set) #old与new交集

for i in set_inter: #更新old列表

old_dic[i] = new_dic[i]

set_new_dife = new_set.difference(old_set) #new里面存在,old里面不存在的集合

for i in set_new_dife:

old_dic[i] = new_dic[i]

set_old_dife = old_set.difference(new_set) #old里面存在,new里面不存在的集合

for i in set_old_dife:

old_dic.pop(i)

print(old_dic)

  




以上是 【python之路11】集合数据类型(set) 的全部内容, 来源链接: utcz.com/z/387425.html

回到顶部