day 7 - 1 集合、copy及基础数据类型汇总
程序员文章站
2022-04-04 09:59:25
集合:{},可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复。(不重要)集合的书写 set = {'alex','wusir','ritian','egon','barry'} 增 add update 删 pop remove clear set 查 for 交集 & inters ......
集合:{},可变的数据类型,他里面的元素必须是不可变的数据类型,无序,不重复。(不重要)
集合的书写
set1 = set({1,2,3}) #set2 = {1,2,3,[2,3],{'name':'alex'}} #错的 print(set1) #print(set2)
set = {'alex','wusir','ritian','egon','barry'}
增 add update
set.add("女神") print(set) set.add("abc") print(set) set.update("abc") print(set)
删 pop remove clear set
set.pop() #随机删除 print(set.pop()) print(set) set.remove('alex') #按元素删除 print(set) set.remove('alex2') print(set) set.clear() #清空列表 set() print(set)
查 for
for i in set: print(i)
交集 & intersection
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} set3 = set1 & set2 print(set3) # {4, 5} print(set1.intersection(set2)) # {4, 5}
并集 | union
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7,8} print(set2.union(set1)) # {1, 2, 3, 4, 5, 6, 7}
反交集 ^ 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}
差集 - difference
set1 = {1,2,3,4,5} set2 = {4,5,6,7,8} print(set1 - set2) # {1, 2, 3} set1独有的 print(set2 - set1) print(set1.difference(set2)) # {1, 2, 3}
子集 < issubset
超集 > issuperset
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超集。
面试题
#去重 li = [1,2,33,33,2,1,4,5,6,6] set1 = set(li) print(set1) li = list(set1) print(li)
让集合变为不可变类型 frozenset 冻结
s1 = {1,2,3} print(s1,type(s1)) #让集合变为不可变类型 frozenset 冻结 s = frozenset('barry') print(s,type(s)) #不可变类型只可以查 for i in s: print(i)
copy
赋值运算
l1 = [1,2,3] l2 = l1 l1.append('a') print(l1,l2) [1, 2, 3, 'a'] [1, 2, 3, 'a']
运用 copy 赋值
l1 = [1,2,3] l2 = l1.copy() print(l1,l2) print(id(l1),id(l2)) l2.append('a') print(l1,l2)
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] 2371695834312 [1, 2, [4, 5, 6], 3] 2371695834440 #第二组 [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] 2371695834184 2371695834184
copy 赋值与数组(2)
l1 = [1,[1],2,3,4] l2 = l1[:] #第一组 l1[1].append('a') #l2 的结果是什么? print(l2) #第二组 print(l1,id(l1)) print(l2,id(l2)) print(l1[1] is l2[1])
#第一组 [1, [1, 'a'], 2, 3, 4] #第二组 [1, [1], 2, 3, 4] 2179834995912 [1, [1], 2, 3, 4] 2179834996040 true
通过定义 copy 赋值
import copy #定义 copy l1 = [1,2,[4,5,6],3] l2 = copys.deepcopy(l1) #第一组 print(l1,id(l1)) print(l2,id(l2)) #第二组 l1[2].append('a') print(l1,l2)
#第一组 [1, 2, [4, 5, 6], 3] 1651493850184 [1, 2, [4, 5, 6], 3] 1651493850696 #第二组 [1, 2, [4, 5, 6, 'a'], 3] [1, 2, [4, 5, 6], 3]
index 与 enumerate 用法
打印序号与值
li = ['alex','taibai','wusir','egon'] for i in li: print(li.index(i),i) for index,i in enumerate(li,1): print(index,i)
基础数据类型汇总
list
列表中元素的删除过程
当每次列表中有元素被删除,则列表的原始下标发生变化
range 的值并不会跟随列表的变化而变化
lis = [11,22,33,44,55] for i in range(len(lis)): print(i) # i = 0 i = 1 i = 2 del lis[i] print(lis) # [11,22,33,44,55] [22, 44, 55] [22, 44]
0 [22, 33, 44, 55] 1 [22, 44, 55] 2 [22, 44] 3 traceback (most recent call last): file "e:/py/day7-1.py", line 4, in <module> del lis[i] indexerror: list assignment index out of range
删除数组中的奇数位
lis = [11,22,33,44,55] #方法一 lis = lis[::2] print(lis) #方法二 l1 = [] for i in lis: if lis.index(i) % 2 == 0: l1.append(i) lis = l1 print(lis) #方法三 for i in range(len(lis)-1,-1,-1): if i % 2 == 1: print(i) del lis[i] print(lis) print(lis)
字典的灵活用法
dic = dict.fromkeys([1,2,3],'春哥') print(dic) dic = dict.fromkeys([1,2,3],[]) #dic = dict.fromkeys([1,2,3],['ysg']) print(dic) # {1: [], 2: [], 3: []} dic[1].append('袁姐') print(dic) dic[2].extend('二哥') print(dic)
数组的灵活用法
l1 = [] l2 = l1 l3 = l1 l3.append('a') print(l1,l2,l3)
不打印含有 k 的键值对
dic = {'k1':'v1','k2':'v2','a3':'v3'} #方法一:字典 dic1 = {} 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)
可以转化成 bool 值
0 '' [] () {} set()
元祖:如果元祖里面只有一个元素且不加逗号,那此元素是什么类型,该元祖就是什么类型。
#第一组 tu1 = (1) tu2 = (1,) print(tu1,type(tu1)) print(tu2,type(tu2)) #第二组 tu1 = ([1]) tu2 = ([1],) print(tu1,type(tu1)) print(tu2,type(tu2)) #元祖的灵活用法 dic = dict.fromkeys([1,2,3,],3) dic[1] = 4 print(dic)
#第一组 1 <class 'int'> (1,) <class 'tuple'> #第二组 [1] <class 'list'> ([1],) <class 'tuple'> #元祖的灵活用法 {1: 4, 2: 3, 3: 3}