标签:one class 返回 logs 数值 sdi 增删改 call err
赋值(=)、浅拷贝(copy)和深拷贝(deepcopy)其实都一样,因为它们永远指向同一个内存地址:
>>> import copy >>> n1 = 123 >>> print(id(n1)) #打印n1的内存地址 502665488 ##赋值## >>> n2 = n1 >>> print(id(n2)) 502665488 ##浅拷贝## >>> n3 = copy.copy(n1) >>> print(id(n3)) 502665488 ##深拷贝## >>> n4 = copy.deepcopy(n1) >>> print(id(n4)) 502665488
赋值只是创建一个变量,该变量指向原来的内存地址
>>> name1 = [‘a‘,‘b‘,[‘m‘,‘n‘],‘c‘] >>> name2 = name1 #输出结果,两个内存地址是一样的 >>> print(id(name1),‘,‘,id(name2)) 50077256 , 50077256
浅拷贝是指在内存地址中,只拷贝出第一层的内存的地址中的内容形成一个新的地址,第二层还是共用同一个内存地址:
>>> import copy >>> name1 = [‘a‘,‘b‘,[‘m‘,‘n‘],‘c‘] #浅copy >>> name2 = copy.copy(name1) >>> print(name1,‘,‘,id(name1)) [‘a‘, ‘b‘, [‘m‘, ‘n‘], ‘c‘] , 50228296 >>> print(name2,‘,‘,id(name2)) [‘a‘, ‘b‘, [‘m‘, ‘n‘], ‘c‘] , 50920008 #修改列表中的元素 >>> name1[0] = ‘h‘ >>> name1[2][0] = ‘M‘ >>> print(name1,‘,‘,id(name1)) [‘h‘, ‘b‘, [‘M‘, ‘n‘], ‘c‘] , 50228296 >>> print(name2,‘,‘,id(name2)) [‘a‘, ‘b‘, [‘M‘, ‘n‘], ‘c‘] , 50920008
浅拷贝是指在内存地址中,拷贝name1第一层和第二层内存地址中的内容形成一个name2的两个新内存地址,两者内存地址不一致,所以无交集
>>> import copy >>> name1 = [‘a‘,‘b‘,[‘m‘,‘n‘],‘c‘] #深拷贝 >>> name2 = copy.deepcopy(name1) >>> print(name1,‘,‘,id(name1)) [‘a‘, ‘b‘, [‘m‘, ‘n‘], ‘c‘] , 50142472 >>> print(name2,‘,‘,id(name2)) [‘a‘, ‘b‘, [‘m‘, ‘n‘], ‘c‘] , 50942280 >>> name1[0] = ‘h‘ >>> name1[2][0] = ‘M‘ >>> print(name1,id(name1),id(name1[2][0])) [‘h‘, ‘b‘, [‘M‘, ‘n‘], ‘c‘] 50142472 10937320 >>> print(name2,id(name2),id(name2[2][0])) [‘a‘, ‘b‘, [‘m‘, ‘n‘], ‘c‘] 50942280 4896280
集合是无序的,天生不重复的数据组合,它的作用如下:
>>> name_1 = [1,2,3,4,7,8,7,10] #把列表转换为集合 >>> name_1 = set(name_1) #转换后,去重 >>> print(name_1,type(name_1)) {1, 2, 3, 4, 7, 8, 10} <class ‘set‘>
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.intersection(name_2) # name_1 & name_2 {8, 1, 10, 3}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.union(name_2)# name_1 | name_2 {1, 2, 3, 4, 5, 7, 8, 10}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.difference(name_2) #name_1 - name_2 {2, 4, 7}
注:差集取的是数值在第一个集合中,但是不在第二个集合中(在我不在你)
把两个集合没有交集的数值取出来
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #输出结果 >>> name_1.symmetric_difference(name_2) #name_1 ^ name_2 {2, 4, 5, 7}
判断一个集合是否是另一个集合的子集
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #输出结果 >>> name_3.issubset(name_1) # name_3 <= name_1 True
判断一个集合是否是另一个集合的父集
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #输出结果 >>> name_1.issuperset(name_3)# name_1 >= name_3 True
判断两个集合是否有交集,没有交集,则返回True
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_3 = [11] >>> name_1 = set(name_1) >>> name_2 = set(name_2) >>> name_3 = set(name_3) #有交集 >>> name_1.isdisjoint(name_2) False #无交集 >>> name_1.isdisjoint(name_3) True
添加(add()) >>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) #添加已存在,不报错 >>> name_2.add(1) >>> name_2 {8, 1, 10, 3, 5} #添加不存在,添加一个新的数值 >>> name_2.add(11) >>> name_2 {1, 3, 5, 8, 10, 11} ###################################### 添加多项(update()) >>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2.update([12,13,14]) #输出结果 >>> name_2 {1, 3, 5, 8, 10, 12, 13, 14} ###################################### 删除(remove(),pop(),discard()) #1、remove() >>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2 {8, 1, 10, 3, 5} >>> name_2.remove(1) #输出 >>> name_2 {8, 10, 3, 5} #删除不存在的元素,会报错 >>> name_2.remove(1) Traceback (most recent call last): File "<input>", line 1, in <module> KeyError: 1 注:用remove删除时,当元素不存在,会报错 #2、pop() >>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2 {8, 1, 10, 3, 5} #输出 >>> name_2.pop() 8 注:pop是随机删除集合中的某个元素,并且打印 #3 discard() >>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2.discard(10) #输出结果 >>> name_2 {8, 1, 3, 5} #删除不存在元素,不报错 >>> name_2.discard(10) 注:用discard删除不存在的元素,不会出现报错 ###################################### 长度(len()) >>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #结果输出 >>> len(name_1) 7 ###################################### #x in s 测试 x 是否是 s 的成员 >>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #结果输出 >>> 1 in name_1 True ###################################### x not in s 测试 x 是否不是 s 的成员 >>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #输出 >>> 12 not in name_1 True
标签:one class 返回 logs 数值 sdi 增删改 call err
原文地址:http://www.cnblogs.com/Keep-Ambition/p/7192587.html