标签:erro stat nothing inf int family object 存在 集合
集合,我们在高中的时候专门学习过集合,并集,交集,差集等,下面来看一下集合的定义,如下:
集合(简称集)是数学中一个基本概念,它是集合论的研究对象,集合论的基本理论直到19世纪才被创立。最简单的说法,即是在最原始的集合论
——朴素集合论中的定义,集合就是“确定的一堆东西”。集合里的“东西”,叫作元素。
由一个或多个确定的元素所构成的整体叫做集合。若x是集合A的元素,则记作x∈A。集合中的元素有三个特征:1.确定性(集合中的元素必须是
确定的) 2.互异性(集合中的元素互不相同。例如:集合A={1,a},则a不能等于1) 3.无序性(集合中的元素没有先后之分),如集合{3,4,5}和
{3,5,4}算作同一个集合。
特性
确定性
给定一个集合,任给一个元素,该元素或者属于或者不属于该集合,二者必居其一,不允许有模棱两可的情况出现。
互异性
一个集合中,任何两个元素都认为是不相同的,即每个元素只能出现一次。有时需要对同一元素出现多次的情形进行刻画,可以使用多重集,其中的元素允许出现多次。
无序性
一个集合中,每个元素的地位都是相同的,元素之间是无序的。集合上可以定义序关系,定义了序关系后,元素之间就可以按照序关系排序。但就集合本身的特性而言,元素之间没有必然的序。
由于集合不是特别常用的功能,因此在Python中创建集合的方法只有一种:
s1 = set()
1.add(self,*args,**kwargs)
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
add(self,*args,**args)是向列表中添加元素,由于列表中的元素是唯一的,下面我们来验证一下:
>>> s1 = set()
>>> s1.add("alex") (1)
>>> s1
{‘alex‘}
>>> s1.add("alex") (2)
>>> s1
{‘alex‘}
>>> s1.add("sb") (3)
>>> s1
{‘alex‘, ‘sb‘}
上述代码中,在(1)处,我们向集合s1中添加元素"alex",打印s1,输出alex;接着我们又向集合中添加元素"alex",然后打印列表s1,输出只有"alex",这就是由于列表的唯一性;在(3)处我们输入了一个不一样的元素,然后打印,在列表中存在这个元素。说明列表中的元素是唯一的。
2.clear(self,*args,**kwargs)
def clear(self, *args, **kwargs): # real signature unknown
""" Remove all elements from this set. """
pass
clear(self)是清除集合中所有的元素,清除集合中所有元素,实例如下:
>>> s1 = {‘alex‘, ‘sb‘}
>>> s1.clear()
>>> s1
set()
当集合中没有元素的时候,显示的是set(),这样避免让我们觉得这是一个空的字典,区分字典。
3.copy(self,*args,**kwargs)
def copy(self, *args, **kwargs): # real signature unknown
""" Return a shallow copy of a set. """
pass
copy(self,*args,**kwargs)复制集合中的元素,产生新的一个内存地址,否则清除集合的时候,关联的其他变量也会被清除掉。实例如下:
>>> s1 = {‘marry‘, ‘alex‘, ‘aoi‘, ‘sb‘}
>>> s2 = s1
>>> s2
{‘marry‘, ‘alex‘, ‘aoi‘, ‘sb‘}
>>> s3 = s1.copy()
>>> s3
{‘marry‘, ‘alex‘, ‘aoi‘, ‘sb‘}
>>> s2.clear()
>>> s1
set()
>>> s2
set()
>>> s3
{‘marry‘, ‘alex‘, ‘aoi‘, ‘sb‘}
从上面代码可以看出,当我们使用赋值关联集合的时候,如果使用等号(=)直接关联,那么使用clear()清除集合的元素的时候,关联的集合中的元素也会被删除,而我们使用copy()就不会出现这样的情况,copy()是在Python内存中重新开辟一个新的内存来存储这个集合。不是直接的关联。
4.difference(self,*args,**kwargs)
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
返回N个集合中不同的元素,形成一个新的集合,
>>> s1 = {‘zeng‘, ‘alex‘, ‘aoi‘, ‘geng‘}
>>> s3 = {‘marry‘, ‘alex‘, ‘aoi‘, ‘sb‘}
>>> s2 = s1.difference(s3)
>>> s2
{‘geng‘, ‘zeng‘}
5.difference_update(self,*args,**kwargs)
def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. """
pass
difference_update(self,*args,**kwargs)s1.difference_update(s3)从集合s1中删除与s3集合中相同的元素。改变了集合s1,而difference()是生成一个新的集合,两者的结果是一样的,difference_update()是更新了集合s1,而difference()是新生成了一个集合,不改变原来的集合。
>>> s1 = {‘zeng‘, ‘alex‘, ‘aoi‘, ‘geng‘}
>>> s3
{‘marry‘, ‘alex‘, ‘aoi‘, ‘sb‘}
>>> s1.difference_update(s3)
>>> s1
{‘zeng‘, ‘geng‘}
>>> s3
{‘marry‘, ‘alex‘, ‘aoi‘, ‘sb‘}
6.discard(self,*args,**kwargs)
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
discard(self,*args,**kwargs)移除集合中的元素,如果这个元素不存在集合中,那么什么都不做。实例如下:
>>> s3 = {‘marry‘, ‘alex‘, ‘aoi‘, ‘sb‘}
>>> s3.discard("alex")
>>> s3
{‘marry‘, ‘aoi‘, ‘sb‘}
>>> s3.discard("tom")
>>> s3
{‘marry‘, ‘aoi‘, ‘sb‘}
7.intersection(self,*args,**kwargs)
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
>>> s1 = {‘zeng‘, ‘geng‘, ‘alex‘, ‘sb‘}
>>> s3 = {‘marry‘, ‘aoi‘, ‘sb‘}
>>> s1.intersection(s3)
{‘sb‘}
s1.intersection(s3)取集合s1和集合s3的交集,返回一个新的集合,不修改原来的集合,intersection_update()取两个集合的交集并修改原来的集合。
8.intersection_update(self,*args,**kwargs)
def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another.(取交集,修改原来set) """
pass
>>> s1 = {‘zeng‘, ‘geng‘, ‘alex‘, ‘sb‘}
>>> s3 = {‘marry‘, ‘aoi‘, ‘sb‘}
>>> s1.intersection_update(s3)
>>> s1
{‘sb‘}
>>> s3
{‘marry‘, ‘aoi‘, ‘sb‘}
s1.intersection_update(s3)取集合s1和集合s3的交集,并把生成的交集元素放到集合s1中,删除两者不是集合的元素。如果两者没有交集则生成一个空的集合。
9.isdisjoint(self,*args,**kwargs)
def isdisjoint(self, *args, **kwargs): # real signature unknown
""" Return True if two sets have a null intersection. """
"""判断是否有交集,如果没有交集,返回True.
pass
isdisjoint(self,*args,**kwargs)判断集合是否有交集,如果没有交集则返回True。如下所示:
>>> s4 = set(["alex"])
>>> s5 = set(["sb"])
>>> s4.isdisjoint(s5)
True
10.issubset(self,*args,**kwargs)
def issubset(self, *args, **kwargs): # real signature unknown
""" Report whether another set contains this set. """
"""判断集合是否是指定集合的子集"""
pass
issubset(self,*args,**kwargs)判断一个集合是否是另外一个集合的子集。实例如下:
>>> s1 = set(("alex","sb","tom","divd"))
>>> s2 = set(("alex","sb"))
>>> s1.issubset(s2)
False
>>> s2.issubset(s1)
True
s1.issubset(s2)判断集合s1是否是集合s2的子集,如果是,则返回True。
11.issuperset(self,*args,**kwargs)
def issuperset(self, *args, **kwargs): # real signature unknown
""" Report whether this set contains another set. """
"""判断是否是父集"""
pass
issuperset(self,*args,**kwargs)判断是否是父集,即s1.issuperset(s2)是判断集合s1包含集合s2,跟issubset()相反,实例如下:
>>> s1 = set(("alex","sb","tom","divd"))
>>> s2 = set(("alex","sb"))
>>> s1.issuperset(s2)
True
>>> s2.issuperset(s1)
False
12.pop(self,*args,**kwargs)
def pop(self, *args, **kwargs): # real signature unknown
"""
Remove and return an arbitrary set element.(移除)
Raises KeyError if the set is empty.
"""
pass
pop(self)从集合中弹出一个元素,并赋值给另外一个变量,实例如下:
>>> s1 = {‘going‘, ‘tom‘, ‘alex‘, ‘is‘, ‘sb‘}
>>> s1.pop()
‘alex‘
>>> s1
{‘going‘, ‘tom‘, ‘is‘, ‘sb‘}
pop(self)不需要参数,表示的是从集合中移除一个元素,并赋值给一个新的变量,移除元素赋值,弹出,不想remove()是从集合中删除元素没有返回值。
13.remove(self,*args,**kwargs)
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
remove(self,member)从集合中移除元素,没有返回值,删除集合中的值,remove(menber),实例如下:
>>> s1 = {‘going‘, ‘tom‘, ‘is‘, ‘sb‘}
>>> s1.remove("is")
>>> s1
{‘going‘, ‘tom‘, ‘sb‘}
remove(menber)是从集合中移除元素,而pop()是从集合中弹出一个元素并赋值给新的变量。两者的区别在于,一个有返回值可以利用,而另外一个是没有返回值的。
14.symmertric_difference(self,*args,**kwargs)
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.)
"""
pass
symmertric_difference(self,*args,**kwargs)返回两个集合的差集,实例如下:
>>> s1 = {‘going‘, ‘tom‘, ‘sb‘}
>>> s2 = set(("tom","Alex"))
>>> s1.symmetric_difference(s2)
{‘going‘, ‘Alex‘, ‘sb‘}
s1.symmetric_difference(s2)返回两个集合的差集,即两个集合交集减去重叠的部分。
15.symmertic_difference_update(self,*args,**kwargs)
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the symmetric difference of itself and another. """
pass
>>> s1 = {‘going‘, ‘tom‘, ‘sb‘}
>>> s2 = {‘tom‘, ‘Alex‘}
>>> s1.symmetric_difference_update(s2)
>>> s1
{‘going‘, ‘Alex‘, ‘sb‘}
>>> s2
{‘tom‘, ‘Alex‘}
返回两个集合的差集。
16.union(self,*args,**kwargs)
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
union(self,*args,**kwargs)两个集合的并集,如下:
>>> s1 = {‘going‘, ‘Alex‘, ‘sb‘}
>>> s2 = {‘tom‘, ‘Alex‘}
>>> s1.union(s2)
{‘going‘, ‘tom‘, ‘Alex‘, ‘sb‘}
17.update(self,*args,**kwargs)
def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
pass
18.__and__(self,*args,**kwargs)
def __and__(self, *args, **kwargs): # real signature unknown
""" Return self&value. """
pass
19.__contains__(self,y)
def __contains__(self, y): # real signature unknown; restored from __doc__
""" x.__contains__(y) <==> y in x. """
pass
20.__getattribute__(self,*args,**kwargs)
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
21.__iand__(self,*args,**kwargs)
def __iand__(self, *args, **kwargs): # real signature unknown
""" Return self&=value. """
pass
22.__ior__(self,*args,**kwargs)
def __ior__(self, *args, **kwargs): # real signature unknown
""" Return self|=value. """
pass
23.__isub__(self,*args,**kwargs)
def __isub__(self, *args, **kwargs): # real signature unknown
""" Return self-=value. """
pass
24.__iter__(self,*args,**kwargs)
def __iter__(self, *args, **kwargs): # real signature unknown
""" Implement iter(self). """
pass
25.__ixor__(self,*args,**kwargs)
def __ixor__(self, *args, **kwargs): # real signature unknown
""" Return self^=value. """
pass
26.__len__(self,*args,**kwargs)
def __len__(self, *args, **kwargs): # real signature unknown
""" Return len(self). """
pass
27.__new__(*args,**kwargs)
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
28.__or__(self,*args,**kwargs)
def __or__(self, *args, **kwargs): # real signature unknown
""" Return self|value. """
pass
29.__rand__(self,*args,**kwargs)
def __rand__(self, *args, **kwargs): # real signature unknown
""" Return value&self. """
pass
30.__reduce__(self,*args,**kwargs)
def __reduce__(self, *args, **kwargs): # real signature unknown
""" Return state information for pickling. """
pass
31.__repr__(self,*args,**kwargs)
def __repr__(self, *args, **kwargs): # real signature unknown
""" Return repr(self). """
pass
32.__ror__(self,*args,**kwargs)
def __ror__(self, *args, **kwargs): # real signature unknown
""" Return value|self. """
pass
33.__rsub__(self,*args,**kwargs)
def __rsub__(self, *args, **kwargs): # real signature unknown
""" Return value-self. """
pass
34.__rxor__(self,*args,**kwargs)
def __rxor__(self, *args, **kwargs): # real signature unknown
""" Return value^self. """
pass
35.__sizeof__(self)
def __sizeof__(self): # real signature unknown; restored from __doc__
""" S.__sizeof__() -> size of S in memory, in bytes """
pass
36.__sub__(self,*args,**kwargs)
def __sub__(self, *args, **kwargs): # real signature unknown
""" Return self-value. """
pass
37.__xor__(self,*args,**kwargs)
def __xor__(self, *args, **kwargs): # real signature unknown
""" Return self^value. """
pass
标签:erro stat nothing inf int family object 存在 集合
原文地址:http://www.cnblogs.com/gengcx/p/6754665.html