标签:format bsp 意义 rand 删除 _for another delattr com
iphone7 = [‘alex‘,‘rain‘,‘jack‘,‘tom‘] iphone8 = [‘alex‘,‘shanshan‘,‘jack‘,‘tom‘] both_list = [] for name in iphone7: if name in iphone8: both_list.append(name) print(both_list) #运行结果 [‘alex‘, ‘jack‘, ‘tom‘]
集合中的元素有三个特征: 1.确定性(元素必须可hash) 2.互异性(去重) 3.无序性(集合中的元素没有先后之分),如集合{3,4,5}和{3,5,4}算作同一个集合。
注意:集合存在的意义就在于去重和关系运算
# 查看所有方法 >>> dir(s) [‘__and__‘, ‘__class__‘, ‘__contains__‘, ‘__delattr__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__getatt ribute__‘, ‘__gt__‘, ‘__hash__‘, ‘__iand__‘, ‘__init__‘, ‘__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‘] >>> # 方法的帮助 >>> help(s.clear) Help on built-in function clear: clear(...) method of builtins.set instance Remove all elements from this set.
(1)创建
(2)添加
(3)删除
>>> iphone7 {‘rain‘, ‘jack‘, ‘alex‘, ‘tom‘} >>> iphone8 {‘shanshan‘, ‘jack‘, ‘alex‘, ‘tom‘} # 交集 >>> iphone7.intersection(iphone8) {‘jack‘, ‘alex‘, ‘tom‘} >>> iphone7 & iphone8 {‘jack‘, ‘alex‘, ‘tom‘} >>> #差集 >>> iphone7.difference(iphone8) {‘rain‘} >>> iphone7 - iphone8 {‘rain‘} >>> >>> iphone8.difference(iphone7) {‘shanshan‘} >>> iphone8 - iphone7 {‘shanshan‘} #并集 >>> iphone7.union(iphone8) {‘rain‘, ‘tom‘, ‘shanshan‘, ‘jack‘, ‘alex‘} >>> iphone7 | iphone8 {‘rain‘, ‘tom‘, ‘shanshan‘, ‘jack‘, ‘alex‘} >>> # 对称差集 # 只买了iphone7 或者iphone8 的人 >>> iphone7.symmetric_difference(iphone8) {‘shanshan‘, ‘rain‘} >>> iphone7 ^ iphone8 {‘shanshan‘, ‘rain‘}
def difference_update(self, *args, **kwargs): # real signature unknown """ Remove all elements of another set from this set. """ pass def intersection_update(self, *args, **kwargs): # real signature unknown """ Update a set with the intersection of itself and another. """ pass
in,not in:判断某元素是否在集合内
==,!=:判断两个集合是否相等
两个集合之间一般有三种关系,相交、包含、不相交。在Python中分别用下面的方法判断:
标签:format bsp 意义 rand 删除 _for another delattr com
原文地址:https://www.cnblogs.com/Mobai-c/p/10147409.html