什麼是元組:只可取,不可更改的列表算法
元組的做用:元組一建立就被寫死了app
定義方式:()內用逗號隔開多個任意數據類型元素code
tup = () # 定義一個空元組 tup2 = (1,) # 當只有一個元素時,必須加逗號 tup3 = (1, 2, 3) tup4 = tuple(元素) # 使用tuple()定義
使用方法:對象
索引取值: [索引位置]
索引
tup=(1,2,3,4,5) print(tup[0])
運行結果:內存
1 Process finished with exit code 0
索引切片: [起始位置:結束位置]
字符串
tup=(1,2,3,4,5) print(tup[0:3])
運行效果:get
(1, 2, 3) Process finished with exit code 0
遍歷: for循環
it
tup=(1,2,3,4,5) for i in tup: print(i)
運行效果:
1 2 3 4 5 Process finished with exit code 0
成員運算: in
or not in
tup=(1,2,3,4,5) print(0 in tup)
運行效果:
False Process finished with exit code 0
長度: len()
tup=(1,2,3,4,5) print(len(tup))
運行效果:
5 Process finished with exit code 0
獲取元素索引: index(元素)
tup=(1,2,3,4,5) print(tup.index(1))
運行效果:
0 Process finished with exit code 0
計數: count(元素)
tup=(1,2,3,4,5) print(tup.count(2))
運行效果:
1 Process finished with exit code 0
有序 or 無序
有序。
可變 or 不可變
元組自己就不可改變,沒有這個說法。
做用:存儲多個具備描述意義的數據
定義方式:{}內用逗號隔開多個鍵key(具備描述意義,不能爲可變數據類型):值value(任意數據類型)對
dic = {'name': 'tom'}
使用方法:
優先掌握:
按key取值/修改值: [key]
/ [key] = 新值
dic = {'a': 1, 'b': 2, 'c': 3} print(dic['a']) # 取出key爲 a 的數據的值 dic['b'] = 4 # 修改key爲 b 的數據的值爲 4 print(dic)
運行結果:
1 {'a': 1, 'b': 4, 'c': 3} Process finished with exit code 0
添加值: [新key] = 新值
dic = {'a': 1, 'b': 2, 'c': 3} dic['d'] = 4 # 添加新的鍵值對,若是key在字典已經存在,則修改其值爲新值 print(dic)
運行結果:
{'a': 1, 'b': 2, 'c': 3, 'd': 4} Process finished with exit code 0
遍歷: for 循環
(只能取出字典的key)
dic = {'a': 1, 'b': 2, 'c': 3} for i in dic: print(i)
運行結果:
a b c d Process finished with exit code 0
成員運算: in
or not in
dic = {'a': 1, 'b': 2, 'c': 3} print('a' in dic)
運行結果:
True Process finished with exit code 0
長度: len()
dic = {'a': 1, 'b': 2, 'c': 3} print(len(dic))
運行結果:
4 Process finished with exit code 0
獲取全部的鍵/值/鍵值對: keys
/ values
/ items
dic = {'a': 1, 'b': 2, 'c': 3} print(dic.keys()) # 獲取全部鍵 print(dic.values()) # 獲取全部值 print(dic.items()) # 獲取全部鍵值對
運行結果:
dict_keys(['a', 'b', 'c']) dict_values([1, 2, 3]) dict_items([('a', 1), ('b', 2), ('c', 3)]) Process finished with exit code 0
運行結果:
可使用 for 循環遍歷獲得全部的鍵值對
dic = {'a': 1, 'b': 2, 'c': 3} for i in dic.items(): print(i)
運行結果:
('a', 1) ('b', 2) ('c', 3) Process finished with exit code 0
進階寫法:
dic = {'a': 1, 'b': 2, 'c': 3} for k, v in dic.items(): # 解壓縮 print(k, v)
運行結果:
a 1 b 2 c 3 Process finished with exit code 0
須要掌握
獲取: .get(key)
dic = {'a': 1, 'b': 2, 'c': 3} print(dic.get('b')) # 獲取 key 爲 b 的值 print(dic.get('d', 0)) # 獲取 key 爲 d 的值,若是沒有,則返回0,能夠自定義,不設置則返回None print(dic.get('d'))
運行結果:
2 0 None Process finished with exit code 0
更新:.update()
dic1 = {'a': 1, 'c': 2} dic2 = {'b': 1, 'd': 2} dic1.update(dic2) print(dic1)
運行結果:
{'a': 1, 'c': 2, 'b': 1, 'd': 2} Process finished with exit code 0
.fromkeys(seq, value)
:以列表 seq 爲 key ,值爲 value ,建立一個新字典
# seq = ('Google', 'Runoob', 'Taobao') 字典鍵值列表 # value -- 可選參數, 設置鍵序列(seq)的值,默新字典的值爲 None seq = ('Google', 'Runoob', 'Taobao') dic = dict.fromkeys(seq) print(dic) dict = dict.fromkeys(seq, 10) print(dic)
運行結果:
{'Google': None, 'Runoob': None, 'Taobao': None} {'Google': 10, 'Runoob': 10, 'Taobao': 10} Process finished with exit code 0
setdefault
:若是鍵不存在於字典中,將會添加鍵並將值設爲默認值
dic = {'a': 1, 'b': 2, 'c': 3} dic.setdefault('j', 2) dic.setdefault('a', 2) print(dic)
運行結果:
{'a': 1, 'b': 2, 'c': 3, 'j': 2} Process finished with exit code 0
有序 or 無序
無序
可變 or 不可變
可變
什麼是集合:無序的不重複元素序列
集合的做用:
定義方式:
定義一個空集合,必須使用 set()
**
s = set() # 空集合
{}內以逗號隔開多個元素(不能可爲可變數據類型)
s = {'a', 'a', 1, 'b', 2, 2, 'c', 3, 3, 4, 5, 6} # 對於數字而言,不會亂序;可是對於其餘,就亂序 print(s)
運行結果:
{1, 2, 3, 4, 5, 6, 'a', 'b', 'c'} Process finished with exit code 0
使用方法
運算
a = set('abracadabra') b = set('alacazam') print(a & b ) # 集合a和b中都包含了的元素 print(a - b ) # 集合a中包含而集合b中不包含的元素 print(a | b ) # 集合a或b中包含的全部元素 print(a ^ b) # 不一樣時包含於a和b的元素
運行結果:
{'c', 'a'} {'d', 'r', 'b'} {'c', 'b', 'z', 'd', 'a', 'r', 'm', 'l'} {'r', 'm', 'b', 'l', 'z', 'd'} Process finished with exit code 0
添加元素: .add(元素)
:若是元素不存在,則添加,若是存在,則不進行任何操做
s = set() s.add(1) print(s)
運行結果:
{1} Process finished with exit code 0
隨機刪除: .pop()
:隨機刪除集合內的一個元素
s = {'a', 'r', 'b', 'c', 'd'} s.pop() print(s)
運行結果:
{'r', 'a', 'b', 'd'} # 第一次運行 {'d', 'b', 'a', 'r'} # 第二次運行 {'d', 'r', 'a', 'c'} #第三次運行 Process finished with exit code 0
有序 or 無序
無序
可變 or 不可變
可變
假設有一個原始列表:lt1 = [1, 2, 3, [4, 5, 6]]
,其中同時包含可變數據類型和不可變數據類型,咱們對其分別進行拷貝、淺拷貝、深拷貝獲得一個新列表,而後改變原始列表的值,觀察新列表。
用列表 lt2
去拷貝列表 lt1
lt1 = [1, 2, 3, [4, 5, 6]] lt2 = lt1
打印兩個列表元素的內存地址以下:
id(lt1) 1789594349192 id(lt1[0]): 140711988719872 id(lt1[1]): 140711988719904 id(lt1[2]): 140711988719936 id(lt1[3]): 1789594349128 id(lt1[3][0]): 140711988719968 id(lt1[3][1]): 140711988720000 id(lt1[3][2]): 140711988720032 ****************************** id(lt2) 1789594349192 id(lt2[0]) 140711988719872 id(lt2[1]) 140711988719904 id(lt2[2]) 140711988719936 id(lt2[3]) 1789594349128 id(lt2[3][0]) 140711988719968 id(lt2[3][1]) 140711988720000 id(lt2[3][2]) 140711988720032 Process finished with exit code 0
lt1
內部的不可變元素髮生改變時,觀察 lt2
lt1 = [1, 2, 3, [4, 5, 6]] lt2 = lt1 lt1.append(7) print(lt1) print(lt2)
打印兩個列表以下:
[1, 2, 3, [4, 5, 6], 7] [1, 2, 3, [4, 5, 6], 7] Process finished with exit code 0
lt1
內部的可變元素髮生改變時,觀察 lt2
lt1 = [1, 2, 3, [4, 5, 6]] lt2 = lt1 lt1[3].append(7) print(lt1) print(lt2)
打印兩個列表以下:
[1, 2, 3, [4, 5, 6, 7]] [1, 2, 3, [4, 5, 6, 7]] Process finished with exit code 0
觀察能夠知道,當 lt2
爲 lt1
的拷貝對象時, lt1
內部的不可變數據發生變化, lt2
也會隨之變化; lt1
內部的可變數據變化, lt2
也會隨之變化。
總結1:當 lt2
爲 lt1
的拷貝對象時,只要 lt1
發生了變化, lt2
都會隨之改變。
用列表 lt2
去淺拷貝列表 lt1
import copy lt1 = [1, 2, 3, [4, 5, 6]] lt2 = copy.copy(lt1) # lt2叫作lt1的淺拷貝對象
打印兩個列表元素的內存地址以下:
id(lt1) 1811455389320 id(lt1[0]): 140712045080832 id(lt1[1]): 140712045080864 id(lt1[2]): 140712045080896 id(lt1[3]): 1811455389256 id(lt1[3][0]): 140712045080928 id(lt1[3][1]): 140712045080960 id(lt1[3][2]): 140712045080992 ****************************** id(lt2) 1811455389576 id(lt2[0]) 140712045080832 id(lt2[1]) 140712045080864 id(lt2[2]) 140712045080896 id(lt2[3]) 1811455389256 id(lt2[3][0]) 140712045080928 id(lt2[3][1]) 140712045080960 id(lt2[3][2]) 140712045080992 Process finished with exit code 0
lt1
內部的不可變元素髮生改變時,觀察 lt2
lt1 = [1, 2, 3, [4, 5, 6]] lt2 = lt1 lt1.append(7) print(lt1) print(lt2)
打印兩個列表以下:
[1, 2, 3, [4, 5, 6], 7] [1, 2, 3, [4, 5, 6]]
lt1
內部的可變元素髮生改變時,觀察 lt2
lt1 = [1, 2, 3, [4, 5, 6]] lt2 = lt1 lt1[3].append(7) print(lt1) print(lt2)
再次打印兩個列表以下:
[1, 2, 3, [4, 5, 6, 7]] [1, 2, 3, [4, 5, 6, 7]]
觀察能夠知道,當 lt2
爲 lt1
的淺拷貝對象時, lt1
內部的不可變數據發生變化時, lt2
不會隨之變化; lt1
內部的可變類型發生變化時, lt2
會隨之變化。
總結2:當 lt2
爲 lt1
的淺拷貝對象時,當 lt1
內部的不可變數據發生變化時, lt2
不會隨之改變,當 lt1
內部的可變數據發生變化時, lt2
會隨之改變。
用列表 lt2
去深拷貝列表 lt1
import copy lt1 = [1, 2, 3, [4, 5, 6]] lt2 = copy.copy(lt1) # lt2叫作lt1的淺拷貝對象
打印兩個列表以下:
id(lt1) 2541724348168 id(lt1[0]): 140712045080832 id(lt1[1]): 140712045080864 id(lt1[2]): 140712045080896 id(lt1[3]): 2541724348104 id(lt1[3][0]): 140712045080928 id(lt1[3][1]): 140712045080960 id(lt1[3][2]): 140712045080992 ****************************** id(lt2) 2541724348360 id(lt2[0]) 140712045080832 id(lt2[1]) 140712045080864 id(lt2[2]) 140712045080896 id(lt2[3]) 2541724819784 id(lt2[3][0]) 140712045080928 id(lt2[3][1]) 140712045080960 id(lt2[3][2]) 140712045080992
lt1
內部的不可變元素髮生改變時,觀察 lt2
lt1 = [1, 2, 3, [4, 5, 6]] lt2 = lt1 lt1.append(7) print(lt1) print(lt2)
打印兩個列表以下:
[1, 2, 3, [4, 5, 6], 7] [1, 2, 3, [4, 5, 6]]
lt1
內部的可變元素髮生改變時,觀察 lt2
lt1 = [1, 2, 3, [4, 5, 6]] lt2 = lt1 lt1[3].append(7) print(lt1) print(lt2)
再次打印兩個列表以下:
[1, 2, 3, [4, 5, 6, 7]] [1, 2, 3, [4, 5, 6]]
觀察能夠知道,當 lt2
爲 lt1
的深拷貝對象時, lt1
內部的不可變數據發生變化時, lt2
不會隨之變化; lt1
內部的可變數據發生變化時, lt2
也不會隨之變化。
總結3:當 lt2
爲 lt1
的深拷貝對象時,不論 lt1
內部的數據有沒有變化, lt2
都不會改變。