【python学习笔记】集合

1. 集合的特点

① 不同元素组成

>>> num = {1,1,2,3,}
>>> num
{1, 2, 3}
>>> 

② 无序

>>> num[2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'set' object does not support indexing
>>> 

③ 集合中的元素必须是不可变类型(数字,字符串,元组)

>>> s = {(1,2,3),3,"dog"}
>>> s
{3, 'dog', (1, 2, 3)}
>>> s = {[1,2,3],3}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> s = {{'dog':1,'cat':2},3}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
>>> 

2. 创建一个元组

① 第一种方法是:直接把一大堆元素用大括号{}括起来

>>> s = {"bird","dog","cat"}
>>> s
{'bird', 'dog', 'cat'}

② 第二种方法:用set()方法

>>> s = set("hello")
>>> set
<class 'set'>
>>> s
{'e', 'o', 'l', 'h'}
>>> 
>>> s = set(["alex","sb"])
>>> s
{'alex', 'sb'}
>>> 

3. 常用的方法

① add():增加一个元素

>>> s = {1,2,3,4,5,6}
>>> s.add("s")
>>> s.add('3')
>>> s.add(3)
>>> s
{1, 2, 3, 4, 5, 6, '3', 's'}

② clear():清除集合内的元素

>>> s.clear()
>>> s
set()
>>> 

③ copy():复制集合

>>> s1 = s.copy()
>>> s1
set()
>>> 

④ pop():随机删除一个元素

>>> s = {1,2,3,4,5,6}
>>> s.pop()
1
>>> s
{2, 3, 4, 5, 6}
>>> 

⑤ remove():指定元素删除,若元素不存在则报错

>>> s = {1,2,3,4,5,6}
>>> s.remove(2)
>>> s
{1, 3, 4, 5, 6}
>>> 

⑥ discard():指定元素删除,若元素不存在不会报错

>>> s = {"sb",2,3,4,(1,2,3)}
>>> s.discard("sb")
>>> s
{2, 3, 4, (1, 2, 3)}
>>> s.discard("girl")
>>> 

4. 其他内置方法

>>> dir(set)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
>>> 

详细如下:

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
        """
        相当于s1-s2
        
        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功能相同,删除元素不存在时不会抛出异常
        
        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
        """
        相当于s1&s2
        
        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. """
        pass

    def issubset(self, *args, **kwargs): # real signature unknown
        """ 
        相当于s1<=s2
        
        Report whether another set contains this set. """
        pass

    def issuperset(self, *args, **kwargs): # real signature unknown
        """
        相当于s1>=s2
        
         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
        """
        相当于s1^s2
        
        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. """
        pass

    def union(self, *args, **kwargs): # real signature unknown
        """
        相当于s1|s2
        
        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

集合的关系运算

in /not in

>>> s1 = {1,2,3,4,5,6}
>>> 1 in s1
True
>>> 7 in s1
False
>>> 2 not in s1
False
>>> 7 not in s1
True
>>> 

② 关系运算

1.交集-----intersection  符号 &

python_l = ['鸡总','老司机','游总','老王']
linux_l = ['鸡总','老王','容哥','大毛']

python_s = set(python_l)
linux_s = set(linux_l)

print(python_s,linux_s)
print(python_s.intersection(linux_s))  #求交集
print(python_s&linux_s) # &作用于intersection一样

打印:
{'游总', '老司机', '老王', '鸡总'} {'大毛', '容哥', '老王', '鸡总'}
{'老王', '鸡总'}
{'老王', '鸡总'}

2.并集----- union  符号 |

print(python_s.union(linux_s))
print(python_s|linux_s)
打印:
{'容哥', '老司机', '大毛', '游总', '老王', '鸡总'}
{'容哥', '老司机', '大毛', '游总', '老王', '鸡总'}
3.差集---- difference 符号 -
print('差集',python_s.difference(linux_s))
print('差集',linux_s.difference(python_s))
print("差集",python_s - linux_s)
print("差集",linux_s - python_s)
打印:
差集 {'游总', '老司机'}
差集 {'容哥', '大毛'}
差集 {'游总', '老司机'}
差集 {'容哥', '大毛'}

4.交叉补集  symmetric_difference  符号 ^

print('交叉补集',python_s.symmetric_difference(linux_s))
print('交叉补集',linux_s.symmetric_difference(python_s))
print('交叉补集',python_s^linux_s)
print('交叉补集',linux_s^python_s)
打印:
交叉补集 {'容哥', '大毛', '游总', '老司机'}
交叉补集 {'游总', '容哥', '大毛', '老司机'}
交叉补集 {'容哥', '大毛', '游总', '老司机'}
交叉补集 {'游总', '容哥', '大毛', '老司机'}

5.difference_update 

python_s = python_s - linux_s
print(python_s)

python_s = python_s.difference(linux_s)
print(python_s)
#difference_update相当于python_s = python_s.difference(linux_s)
python_s.difference_update(linux_s)
print(python_s)
打印:
{'老司机', '游总'}
{'游总', '老司机'}
{'游总', '老司机'}

6.intersection_update 更新intersection后的集合

python_s = python_s.intersection(linux_s)
print(python_s)

python_s.intersection_update(linux_s)
print(python_s)
打印:
{'鸡总', '老王'}
{'鸡总', '老王'}

7.symmetric_difference_update 

python_s.symmetric_difference_update(linux_s)
print(python_s)
打印:
{'容哥', '大毛', '游总', '老司机'}
8.isdisjoint 若两个集合交集为空,则返回True

s1 = {1,2}
s2 = {3,5}
print(s1.isdisjoint(s2))
打印:
True

9.issubset 一个集合是例外一个集合的子集,若为真,则返回True

s1 = {1,2}
s2 = {1,2,3,4}
print(s1.issubset(s2))
print(s2.issubset(s1))
打印:
True
False

10.update  更新集合多个值,用于增加集合元素  参数只能是可迭代对象,与add()不同

s1 = {1,2}
s2 = {1,2,3,4}
s1.update(s2)
print(s1)
s1.update((5,6))
print(s1)
s1.update([7,8])
print(s1)
s1.update('hello')
print(s1)
打印:
{1,2,3,4}
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4, 5, 6, 7, 8}
{'o', 1, 2, 3, 4, 5, 6, 7, 8, 'l', 'h', 'e'}

5.不可变集合

有些时候我们希望集合中的数据是稳定的,像元组一样不能随意修改删除集合中的元素,我们可以定义不可变集合,使用frozenset()函数

>>> set_test = set('hello')
>>> set_test
{'e', 'h', 'o', 'l'}
>>> s1 = frozenset(set_test)
>>> print(s1)
frozenset({'e', 'h', 'o', 'l'})
>>> s1.add('p')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
>>> s1.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'pop'

作业题:

1.有列表l=['a','b',1,'a','a'],列表元素均为可hash类型,去重,得到新列表,且新列表无需保持列表原来的顺序

l=['a','b',1,'a','a']
print(set(l))
2.在上题的基础上,保存列表原来的顺序

l=['a','b',1,'a','a']
l1 = []
for i in l:
    if i not in l1:
	l1.append(i)
print(l1)
l=['a','b',1,'a','a']
s = set()
l1 = []
for i in l:
    if i not in s:
	s.add(i)
	l1.append(i)
print(l1)
3.有如下列表,列表元素为不可hash类型,去重,得到新列表,且新列表一定要保持列表原来的顺序
l=[
    {'name':'egon','age':18,'sex':'male'},
    {'name':'alex','age':73,'sex':'male'},
    {'name':'egon','age':20,'sex':'female'},
    {'name':'egon','age':18,'sex':'male'},
    {'name':'egon','age':18,'sex':'male'},
]


l=[
    {'name':'egon','age':18,'sex':'male'},
    {'name':'alex','age':73,'sex':'male'},
    {'name':'egon','age':20,'sex':'female'},
    {'name':'egon','age':18,'sex':'male'},
    {'name':'egon','age':18,'sex':'male'},
]

s = set()
l1 = []

for i in l:
    item = (i['name'],i['age'],i['sex'])
    if item not in s:
	s.add(item)
	l1.append(item)
print(l1)

猜你喜欢

转载自blog.csdn.net/mr_humi/article/details/80848975
今日推荐