Python高手之路【三】python基础之函数

set 是一个无序且不重复的元素集合,基本数据类型补充:,intersection取得两个集合中的交集元素,并将这些元素以一个新的集合返回给一个变量接收,intersection_update取得两个集合的交集元素,并更新a集合,collections系列,issubset判断是否为子集,issuperset判断是否为父集,三者都能达到移除元素的效果,区别在于remove移除集合中不存在的元素时会报错,discard移除不存在的元素是不会报错,pop无法精确控制移除哪个元素,按其自身的规则随机移除元素,返回被移除的元素,可以使用变量接收其返回值,symmetric_difference返回两个集合中不是交集的元素,上面的代码中,将symmetric_difference换成symmetric_difference_update则表示将两个集合中不是交集的部分赋值给s,三元运算,1:创建,union方法合并两个集合,update方法更新s集合,将b集合中的元素添加到s集合中!update方法也可以传递一个列表,如:update([23,45,67]),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. A中存在,B中不存在

(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. 从当前集合中删除和B中相同的元素”””
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. 取交集并更更新到A中 “””
pass

def isdisjoint(self, *args, **kwargs): # real signature unknown
“”” Return True if two sets have a null intersection. 如果没有交集,返回True,否则返回False”””
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. 移除元素
“””
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
“””
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. 对称差集,并更新到a中 “””
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,s = set()
s = {11,22,33,55},要求:,2:转换,1)两个字典中有相同键的,则将new_dict中的值更新到old_dict对应键的值,li = [11,22,33,44]
tu = (11,22,33,44)
st = ”
s = set(li),2)old_dict中存在的键且new_dict中没有的键,在old_dict中删除,并把new_dict中的键值更新到old_dict中,a = {11,22,33,44}
b = {22,66,77,88}
ret = a.intersection(b)
print(ret),3)最后输出old_dict,Counter是对字典类型的补充,用于追踪值的出现次数。,ps:具备字典的所有功能 + 自己的功能,3:intersection , intersection_update方法,orderdDict是对字典类型的补充,他记住了字典元素添加的顺序,defaultdict是对字典的类型的补充,他默认给字典的值设置了一个类型。,使用方法:,根据nametuple可以创建一个包含tuple所有功能以及其他功能的类型。,一个线程安全的双向队列,a = {11,22,33,44}
b = {22,66,77,88}
a.intersection_update(b)
print(a),注:既然有双向队列,也有单项队列(先进先出 FIFO ),s = {11,22,33,44}
b = {11,22,77,55}
ret = s.isdisjoint(b)#有交集返回False,没有交集返回True
print(ret)
## False,三元运算(三目运算),是对简单的条件语句的缩写。,对于 数字 和 字符串 而言,赋值、浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址。,a = {11,22,33,44}
b = {11,44}
ret = b.issubset(a)
print(ret)
##########################################
True,Python高手之路【三】python基础之函数,a = {11,22,33,44}
b = {11,44}
ret = a.issubset(b)
print(ret)
##########################################
False

1. 本站所有资源来源于用户上传和网络,因此不包含技术服务请大家谅解!如有侵权请邮件联系客服!10210454@qq.com
2. 本站不保证所提供所有下载的资源的准确性、安全性和完整性,资源仅供下载学习之用!如有链接无法下载、失效或广告,请联系客服处理,有奖励!
3. 您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容资源!如用于商业或者非法用途,与本站无关,一切后果请用户自负!
4. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有RB奖励和额外RMB收入!

磊宇堂正在使用的服务器 维护管理由磊宇云服务器提供支持

磊宇堂 » Python高手之路【三】python基础之函数

提供最优质的资源集合

立即查看 了解详情