python3集合方法统计
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python3集合方法统计相关的知识,希望对你有一定的参考价值。
1、update()
- 官方说明:
def update(self, *args, **kwargs): # real signature unknown """ Update a set with the union of itself and others. """ pass
描述:扩展集合
参数:要添加的集合
返回值:None(原集合会被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘sky‘} s3 = s1.update(s2) # 扩展集合s1 print(type(s3),s3) # 查看返回值 print(type(s1),s1) # 打印扩展后的集合s1
输出结果:
<class ‘NoneType‘> None <class ‘set‘> {‘knight‘, ‘sky‘, ‘pudding‘, ‘lisa‘, ‘william‘}
2、copy()
- 官方说明:
def copy(self, *args, **kwargs): # real signature unknown """ Return a shallow copy of a set. """ pass
描述:复制集合
参数:无
返回值:返回一个和原集合一样的新的集合
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.copy() # 对集合s1进行复制 print(type(s1),s1) print(type(s2),s2)
输出结果:
<class ‘set‘> {‘knight‘, ‘pudding‘, ‘william‘, ‘lisa‘} <class ‘set‘> {‘knight‘, ‘pudding‘, ‘william‘, ‘lisa‘}
3、pop()
- 官方说明:
def pop(self, *args, **kwargs): # real signature unknown """ Remove and return an arbitrary set element. Raises KeyError if the set is empty. """ pass
描述:随机删除集合中的一个元素
参数:无
返回值:返回被删除的元素
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.pop() # 随机删除集合中的一个元素 print(type(s1),s1) print(type(s2),s2)
输出结果:
<class ‘set‘> {‘lisa‘, ‘knight‘, ‘william‘} <class ‘str‘> pudding
4、clear()
- 官方说明:
def clear(self, *args, **kwargs): # real signature unknown """ Remove all elements from this set. """ pass
描述:清空字典
参数:无
返回值:None(原集合会被修改)
示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.clear() # 清空集合 print(type(s1),s1) print(type(s2),s2)
输出结果:
<class ‘set‘> set() <class ‘NoneType‘> None
5、remove()
- 官方说明:
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
描述:删除集合中指定的元素
参数:element 元素
返回值:None(原集合会被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.remove(‘lisa‘) print(type(s1),s1) print(type(s2),s2) # 返回值为空
输出结果:
<class ‘set‘> {‘william‘, ‘pudding‘, ‘knight‘} <class ‘NoneType‘> None
6、add()
- 官方说明:
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
描述:为集合增加元素
参数:element 元素
返回值:None(原集合会被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.add(‘sky‘) print(type(s1),s1) print(type(s2),s2) # 返回值为空
输出结果:
<class ‘set‘> {‘pudding‘, ‘lisa‘, ‘william‘, ‘knight‘, ‘sky‘} <class ‘NoneType‘> None
7、difference()
- 官方说明:
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
描述:差集运算,原集合不更新
参数:set 要对比的集合
返回值:得到一个差集
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘sky‘,‘william‘,‘hello‘,‘knight‘} s3 = s1.difference(s2) print(type(s3),s3) # 得到一个差集。 print(type(s1),s1) # 原集合不更新
输出结果:
<class ‘set‘> {‘lisa‘, ‘pudding‘} <class ‘set‘> {‘pudding‘, ‘lisa‘, ‘knight‘, ‘william‘}
8、difference_update
- 官方说明:
def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. """ pass
描述:差集运算,原集合更新
参数:set 要对比的集合
返回值:None(原集合会被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘sky‘,‘william‘,‘hello‘,‘knight‘} s3 = s1.difference_update(s2) print(type(s3),s3) # 返回None print(type(s1),s1) # 原集被更新
输出结果:
<class ‘NoneType‘> None <class ‘set‘> {‘pudding‘, ‘lisa‘}
9、discard()
- 官方说明:
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
描述:删除集合中指定的元素
参数:element 元素
返回值:None(原集合会被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = s1.discard(‘william‘) print(type(s2),s2) print(type(s1),s1)
输出结果:
<class ‘NoneType‘> None <class ‘set‘> {‘lisa‘, ‘knight‘, ‘pudding‘}
10、intersection()
- 官方说明:
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
描述:交集运算,原集合不更新
参数:set 要对比的集合
返回值:得到一个交集
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘xxxx‘} s3 = s1.intersection(s2) print(type(s3),s3) # 得到一个交集 print(type(s1),s1) # 原集合不更新
输出结果:
<class ‘set‘> {‘william‘} <class ‘set‘> {‘william‘, ‘lisa‘, ‘knight‘, ‘pudding‘}
11、intersection_update()
- 官方说明:
def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. """ pass
描述:交集运算,原集合更新
参数:set 要对比的集合
返回值:None(原集合会被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘xxxx‘} s3 = s1.intersection_update(s2) print(type(s3),s3) # 返回None print(type(s1),s1) # 原集合更新
输出集合:
<class ‘NoneType‘> None <class ‘set‘> {‘william‘}
12、isdisjoint()
- 官方说明:
def isdisjoint(self, *args, **kwargs): # real signature unknown """ Return True if two sets have a null intersection. """ pass
描述:判断是否有交集,如果有交集则返回False,没有返回True
参数:set 要对比的集合
返回值:返回一个布尔值
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘xxxx‘,‘lisa‘} s3 = s1.isdisjoint(s2) # 有交集则返回False print(type(s3),s3) #-------------------------------------------- s4 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s5 = {‘xxxx‘,‘yyyyy‘,‘kkkkkk‘,‘uuuuuu‘} s6 = s4.isdisjoint(s5) # 没有交集则返回True print(type(s6),s6)
输出结果:
<class ‘bool‘> False <class ‘bool‘> True
13、issubset()
- 官方说明:
def issubset(self, *args, **kwargs): # real signature unknown """ Report whether another set contains this set. """ pass
描述:判断原集合是否是要对比的集合的子集,如果是则返回True,否则返回False
参数:set 要对比的集合
返回值:得到一个布尔值
- 示例:
s1 = {‘william‘,‘pudding‘} s2 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s3 = s1.issubset(s2) # s1是否是s2的子集,所以返回True print(type(s3),s3) #-------------------------------------- s4 = {‘william‘,‘xxxxx‘} s5 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s6 = s4.issubset(s5) # s4不是s5的子集,所以返回False print(type(s6),s6)
输出结果:
<class ‘bool‘> True <class ‘bool‘> False
14、issuperset()
- 官方说明:
def issuperset(self, *args, **kwargs): # real signature unknown """ Report whether this set contains another set. """ pass
描述:判断原集合是否是要对比的集合的超(父)集,如果是则返回True,否则返回False
参数:set 要对比的集合
返回值:得到一个布尔值
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘pudding‘} s3 = s1.issuperset(s2) # s1是s2的超(父)集,所以返回True print(type(s3),s3) #-------------------------------------- s4 = {‘william‘,‘xxxxx‘} s5 = {‘william‘,‘lisa‘,‘nnnnn‘,‘xxxx‘} s6 = s4.issuperset(s5) # s4不是s5的超(父)集,所以返回True print(type(s6),s6)
输出结果:
<class ‘bool‘> True <class ‘bool‘> False
15、symmetric_difference()
- 官方说明:
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
描述:对称差集运算,原集合不更新
参数:set 要对比的集合
返回值:返回一个对称差集
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘pudding‘,‘xxxxxx‘} s3 = s1.symmetric_difference(s2) print(type(s3),s3) # 得到一个对称差集 print(type(s1),s1) # 对称差集运算,原集合不更新
输出结果:
<class ‘set‘> {‘knight‘, ‘lisa‘, ‘xxxxxx‘} <class ‘set‘> {‘knight‘, ‘william‘, ‘lisa‘, ‘pudding‘}
16、symmetric_difference_update()
- 官方说明:
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown """ Update a set with the symmetric difference of itself and another. """ pass
描述:对称差集运算,原集合更新
参数:set 要对比的集合
返回值:None(原集合会被修改)
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘pudding‘,‘xxxxxx‘} s3 = s1.symmetric_difference_update(s2) print(type(s3),s3) # 返回None print(type(s1),s1) # 对称差集运算,原集合更新
输出结果:
<class ‘NoneType‘> None <class ‘set‘> {‘lisa‘, ‘knight‘, ‘xxxxxx‘}
17、union()
- 官方说明:
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
描述:并集运算,原集合不更新
参数:set 要对比的集合
返回值:得到一个新的集合,两个集合中都有的元素都合并成一个。
- 示例:
s1 = {‘william‘,‘lisa‘,‘knight‘,‘pudding‘} s2 = {‘william‘,‘pudding‘,‘xxxxxx‘} s3 = s1.union(s2) print(type(s3),s3) # 返回得到一个新的集合,两个集合中都有的元素都合并成一个。 print(type(s1),s1) # 并集运算,原集合不更新
输出结果:
<class ‘set‘> {‘xxxxxx‘, ‘knight‘, ‘william‘, ‘lisa‘, ‘pudding‘} <class ‘set‘> {‘pudding‘, ‘lisa‘, ‘knight‘, ‘william‘}
以上是关于python3集合方法统计的主要内容,如果未能解决你的问题,请参考以下文章
[ jquery 文档处理 insertBefore(content) before(content|fn) ] 此方法用于把所有匹配的元素插入到另一个指定的元素元素集合的前面,实现外部插入(代码片段