2.10 基本数据类型——集合
本节重点
引子
l= ['张三','李四','老男孩']
p = ['张三','李四','alex']
l_p = []
for i in l:
if i in p:
l_p.append(i)
print(l_p)认识集合
用集合解决问题

集合的定义
集合的关系运算




集合的常用操作
集合的工厂函数
Last updated
l= ['张三','李四','老男孩']
p = ['张三','李四','alex']
l_p = []
for i in l:
if i in p:
l_p.append(i)
print(l_p)




Last updated
l= {'张三','李四','老男孩'} #集合定义
p = {'张三','李四','alex'}
l_p = l&p #集合求交集
print(l_p)l= {1,2,3,1} #此处应说明集合“去重”的效果
#定义可变集合
>>> set_test=set('hello') #此处应说明集合的“无序性”
>>> set_test
{'l', 'o', 'e', 'h'}
#改为不可变集合frozenset
>>> f_set_test=frozenset(set_test)
>>> f_set_test
frozenset({'l', 'e', 'h', 'o'})l= {'张三','李四','老男孩'}
p = {'张三','李四','alex'}
print(l.intersection(p))
print(l&p)l= {'张三','李四','老男孩'}
p = {'张三','李四','alex'}
print(l.union(p))
print(l|p)l= {'张三','李四','老男孩'}
p = {'张三','李四','alex'}
print(l.difference(p))
print(l-p)a = {1,2,3}
b = {2,3,4,5}
print(a.symmetric_difference(b))
print(a^b)>>> a={1,2}
>>> a.update([3,4],[1,2,7])
>>> a
{1, 2, 3, 4, 7}
>>> a.update("hello")
>>> a
{1, 2, 3, 4, 7, 'h', 'e', 'l', 'o'}
>>> a.add("hello")
>>> a
{1, 2, 3, 4, 'hello', 7, 'h', 'e', 'l', 'o'}>>> a={1,2,3,4}
>>> a.discard(1)
>>> a
{2, 3, 4}
>>> a.discard(1)
>>> a
{2, 3, 4}
>>> a.remove(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 1>>> a={3,"a",2.1,1}
>>> a.pop()
>>> a.pop()
>>> a.clear()
>>> a
set()
>>> a.pop()
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 'pop from an empty 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
"""
相当于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
...略...