集合:{},可變的資料型別,他裡面的元素必須是不可變的資料型別,無序,不重複。(不重要)
集合的書寫
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 del
set.pop() #隨機刪除 print(set.pop()) print(set) set.remove(`alex`) #按元素刪除 print(set) set.remove(`alex2`) print(set) set.clear() #清空列表 set() print(set) del 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}