标签:upd closed 赋值运算 type abc rate 一个 diff nbsp
1.在列表和字典的循环中,不能直接删除它们
删除第1、3位的元素 lis = [11,22,33,44,55]
#方法1 ‘‘‘lis = [11,22,33,44,55] li = [] for i in range(len(lis)): if i % 2 == 0: li.append(lis[i]) lis = li print(li) ‘‘‘ #方法2 lis = [11,22,33,44,55] tu = tuple(lis) for i in range(len(lis)): if i % 2 == 1: lis.remove(tu[i]) print(lis)
删除字典dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘a3‘:‘v3‘}中相应的键值对
# dic1 = {} dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘a3‘:‘v3‘} # for i in dic: # if ‘k‘ not in i: # dic1.setdefault(i,dic[i]) # dic = dic1 # print(dic) # l = [] # for i in dic: # if ‘k‘ in i: # l.append(i) # for i in l: # del dic[i] # print(dic)
元组
tu1 = (1) int
tu2 = (1,) tuple
2.集合
集合是可变的,它里面的元素不可变,集合是无序的且不重复的
集合有增加、删除、查询、交并差等操作
1.增加
.add() / .update()
# set1 = set({1,2,3}) # set2 = {1,2,3,[3,4],{‘name‘:‘alwx‘}} #错的 # print(set1,set2) set1 = {‘alex‘,‘wusir‘,‘ritian‘,‘egon‘,‘barry‘,‘barrry‘} #add 加一个元素 set1.add(‘女生‘) print(set1) #update 迭代添加 set1.update(‘abc‘) print(set1)
2.删除
.pop() / .remove() / del /.clear()
# #删除 pop # print(set1.pop()) #随机删除,有返回值 # print(set1) # # #按元素删除 remove # set1.remove(‘alex‘) # print(set1) #清空集合 # set1.clear() # print(set1) #del 删除整个集合 # del set1 # print(set1)
3.没有改,查只能按照for循环来查,因为集合是无序的
for i in set1: print(i)
4.交集,并集,反交集,差集,子集,超集
&, .intersection()
| , .union()
^ , .symmetric_difference()
set1 - set2 , .difference()
set1 < set2 返回bool值,就是比较集合的包含关系
#特殊之处 可以求交并差集 #交集 set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 & set2) #{4,5} print(set1.intersection(set2)) #{4,5} set3 = set1 & set2 #并集 print(set1 | set2) print(set2.union(set1)). #反交集 除了交集之外的并集 print(set1 ^ set2) print(set1.symmetric_difference(set2)) #差集 print(set1 - set2) #set1独有的 print(set1.difference(set2)) #子集与超集 比较包含关系,返回bool值 set1 = {1,2,3} set2 = {1,2,3,4,5} print(set1 < set2) print(set1.issubset(set2))
5.例题 去除列表中的重复元素
li = [1,2,33,33,2,1,4,5,6,6] set1 = set(li) print(set1) li = list(set1) print(li)
集合可以冻结,变成不可变数据类型
#set 本身是可变的,内部元素不可变,将set冻结 s = frozenset(‘barry‘) #变成不可变数据类型,只读,无序 print(s,type(s)) for i in s: print(i)
6.深浅copy
赋值运算后,两个变量指向同一个内存地址,改变一个会引起另外一个的改变
copy运算后,两个变量内存地址不相同,改变 一个不会引起另外一个的改变
l1 = [1,2,3] l2 = l1 l1.append(‘a‘) print(l1,l2) #copy l1 = [1,2,3] l2 = l1.copy() print(l1,l2) print(id(l1),id(l2)) l2.append(‘a‘) print(l1,l2) [1, 2, 3, ‘a‘] [1, 2, 3, ‘a‘] [1, 2, 3] [1, 2, 3] 1921628470728 1921628348040 [1, 2, 3] [1, 2, 3, ‘a‘]
copy运算后,里面 列表 的内存地址不变
l1 = [1,2,[4,5,6],3] l2 = l1.copy() print(l1,id(l1)) print(l2,id(l2)) l1.append(‘a‘) print(l1,l2) l1[2].append(‘a‘) print(l1,l2) print(id(l1[2])) print(id(l2[2])) ################### [1, 2, [4, 5, 6], 3] 1921627608072 [1, 2, [4, 5, 6], 3] 1921628470728 [1, 2, [4, 5, 6], 3, ‘a‘] [1, 2, [4, 5, 6], 3] [1, 2, [4, 5, 6, ‘a‘], 3, ‘a‘] [1, 2, [4, 5, 6, ‘a‘], 3] 1921628470856 1921628470856
深度copy 列表的内存地址改变了,两个列表不相互影响
但是 l2 = l1[:] 赋值运算会导致两个列表的内存地址不一样,但是值却会相互影响而改变
import copy l1 = [1,2,[4,5,6],3] l2 = copy.deepcopy(l1) print(l1,id(l1)) print(l2,id(l2)) l1[2].append(‘a‘) print(l1,l2) l1 = [1,[1],2,3,4] l2 = l1[:] ********************** l1[1].append(‘a‘) print(l1,id(l1)) print(l2,id(l2)) print(l1[1] is l2[1]) ######################### [1, 2, [4, 5, 6], 3] 1921630006344 [1, 2, [4, 5, 6], 3] 1921627608072 [1, 2, [4, 5, 6, ‘a‘], 3] [1, 2, [4, 5, 6], 3] [1, [1, ‘a‘], 2, 3, 4] 1921628470856 [1, [1, ‘a‘], 2, 3, 4] 1921630006344 True
循环输出列表元素和它的序号
li = [‘alex‘,‘taibai‘,‘wusir‘,‘egon‘] for i in li: print(li.index(i) + 1,i) for index,i in enumerate(li,1): print(index,i)
标签:upd closed 赋值运算 type abc rate 一个 diff nbsp
原文地址:https://www.cnblogs.com/benbenxiaofeifei/p/9348342.html