标签:迭代 哈希 开始 IV bsp 改变 union imp import
集合是无序的,不重复的数据集合,其元素为可哈希(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)。以下是集合最重要的两点:
去重,把一个列表变成集合,就自动去重了。
关系测试,测试两组数据之前的交集、差集、并集等关系。
1、集合的创建
set1 = set({1,2,‘barry‘}) set2 = {1,2,‘barry‘} print(set1,set2) # {1, 2, ‘barry‘} {1, 2, ‘barry‘}
2、集合的增
set1 = {‘alex‘,‘wusir‘,‘ritian‘,‘egon‘,‘barry‘} set1.add(‘景女神‘) print(set1) #update:迭代着增加 set1.update(‘A‘) print(set1) set1.update(‘老师‘) print(set1) set1.update([1,2,3]) print(set1)
3、集合的删
set1 = {‘alex‘,‘wusir‘,‘ritian‘,‘egon‘,‘barry‘} set1.remove(‘alex‘) # 删除一个元素 print(set1) set1.pop() # 随机删除一个元素 print(set1) set1.clear() # 清空集合 print(set1) del set1 # 删除集合 print(set1)
4、集合的其他操作
4.1 交集(& 或者intersection)
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 & set2) # {4, 5} print(set1.intersection(set2)) # {4, 5}
4.2 并集 ( | 或者union)
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7} print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7}
4.3 差集 ( - 或者 difference)
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 - set2) # {1, 2, 3} print(set1.difference(set2)) # {1, 2, 3}
4.4 反交集 (^ 或者 symmetric_difference)
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 ^ set2) # {1, 2, 3, 6, 7, 8} print(set1.symmetric_difference(set2)) # {1, 2, 3, 6, 7, 8}
4.5 子集与超集
set1 = {1,2,3} set2 = {1,2,3,4,5,6} print(set1 < set2) print(set1.issubset(set2)) # 这两个相同,都是说明set1是set2子集。 print(set2 > set1) print(set2.issuperset(set1)) # 这两个相同,都是说明set2是set1超集。
5、frozenset不可变集合,让集合变成不可变类型。
s = frozenset(‘barry‘) print(s,type(s)) # frozenset({‘a‘, ‘y‘, ‘b‘, ‘r‘}) <class ‘frozenset‘>
1、赋值运算
l1 = [1,2,3,[‘barry‘,‘alex‘]] l2 = l1 l1[0] = 111 print(l1) # [111, 2, 3, [‘barry‘, ‘alex‘]] print(l2) # [111, 2, 3, [‘barry‘, ‘alex‘]] l1[3][0] = ‘wusir‘ print(l1) # [111, 2, 3, [‘wusir‘, ‘alex‘]] print(l2) # [111, 2, 3, [‘wusir‘, ‘alex‘]]
对于赋值运算来说,l1与l2指向的是同一个内存地址,所以他们是完全一样的。
2、浅拷贝copy
l1 = [1,2,3,[‘barry‘,‘alex‘]] l2 = l1.copy() print(l1,id(l1)) # [1, 2, 3, [‘barry‘, ‘alex‘]] 2380296895816 print(l2,id(l2)) # [1, 2, 3, [‘barry‘, ‘alex‘]] 2380296895048 l1[1] = 222 print(l1,id(l1)) # [1, 222, 3, [‘barry‘, ‘alex‘]] 2593038941128 print(l2,id(l2)) # [1, 2, 3, [‘barry‘, ‘alex‘]] 2593038941896 l1[3][0] = ‘wusir‘ print(l1,id(l1[3])) # [1, 2, 3, [‘wusir‘, ‘alex‘]] 1732315659016 print(l2,id(l2[3])) # [1, 2, 3, [‘wusir‘, ‘alex‘]] 1732315659016
对于浅copy来说,第一层创建的是新的内存地址,而从第二层开始,指向的都是同一个内存地址,所以,对于第二层以及更深的层数来说,保持一致性。
3、深拷贝deepcopy
import copy l1 = [1,2,3,[‘barry‘,‘alex‘]] l2 = copy.deepcopy(l1) print(l1,id(l1)) # [1, 2, 3, [‘barry‘, ‘alex‘]] 2915377167816 print(l2,id(l2)) # [1, 2, 3, [‘barry‘, ‘alex‘]] 2915377167048 l1[1] = 222 print(l1,id(l1)) # [1, 222, 3, [‘barry‘, ‘alex‘]] 2915377167816 print(l2,id(l2)) # [1, 2, 3, [‘barry‘, ‘alex‘]] 2915377167048 l1[3][0] = ‘wusir‘ print(l1,id(l1[3])) # [1, 222, 3, [‘wusir‘, ‘alex‘]] 2915377167240 print(l2,id(l2[3])) # [1, 2, 3, [‘barry‘, ‘alex‘]] 2915377167304
对于深copy来说,两个是完全独立的,改变任意一个的任何元素(无论多少层),另一个绝对不改变。
标签:迭代 哈希 开始 IV bsp 改变 union imp import
原文地址:https://www.cnblogs.com/lijiejoy/p/9153002.html