Python認爲一切皆爲對象;好比咱們初始化一個list時:python
li = list('abc')
其實是實例化了內置模塊builtins(python2中爲__builtin__模塊)中的list類;git
class list(object): def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) -> new list initialized from iterable's items # (copied from class doc) """ pass
驗證:python3.x
In [1]: li = list('abc') In [2]: li.__class__ Out[2]: list In [3]: li.__class__.__name__ Out[3]: 'list'
python使用對象模型來存儲數據。構造任何類型的值都是一個對象。全部python對象都擁有三個特性:身份、類型、值:api
1)身份:每一個對象都有一個惟一的身份標識本身,任何對象的身份可使用內建函數 id() 來獲得。安全
2)類型:對象的類型決定了該對象能夠保存什麼類型的值,能夠進行什麼樣的操做,以及遵循什麼樣的規則。可使用 type() 函數查看python對象的類型。type()返回的是對象而不是簡單的字符串。數據結構
3)值:對象表示的數據。app
python不須要事先聲明對象類型,當python運行時,會根據「=「右側的數據類型來肯定變量類型;ide
(1)標準類型(基本數據類型)函數
a) .數字(分爲幾個字類型,其中三個是整型)ui
b) String 字符串
c) List 列表
d) Tuple 元祖
e) Dictionary 字典
(2)其餘內建類型
類型、Null對象(None)、文件、集合/固定集合、函數/方法、模塊、類
有3種不一樣的模型能夠幫助咱們對基本類型進行分類
1)存儲類型
一個能保存單個字面對象的類型,稱爲原子或標量存儲;能夠容納多個對象的類型,稱爲容器存儲;而且全部的python容器對象均可以容納不一樣類型的對象;
注意,python沒有字符類型,因此雖然字符串你看上去像容器類型,實際是原子類型
2)更新模型(是否可變)
這裏所說的是否可變,可更新是針對的對象的值而言的;是在對象身份不變的狀況下(id不變),值是否可更新;
s = 'abc' print('id of s: ', id(s)) s = 'xyz' print('id of s: ', id(s))
結果:
id of s: 41643960
id of s: 46319184
上例中,不少人會說s是字符串,也可變啊,可是注意,這裏s指向的對象發生了變化(id值發生了變化),因此這不是咱們所說的可更新;讓咱們看一下list
li = ['a', 'b'] print('li before update', li) print('id of li: ', id(li)) li[0] = 'x' print('li after update', li) print('id of li: ', id(li))
結果:
li before update ['a', 'b'] id of li: 48447304 li after update ['x', 'b'] id of li: 48447304
li的值發生了變化,可是li仍是原來的對象(id值不變)
另外,對於元祖,元祖自己不可變,可是當元祖存在可變元素時,元祖的元素可變,好比
t = (1, ['a','b']) print('t before update', t) print('id of t: ', id(t)) t[1][0] = 'x' print('t after update', t) print('id of t: ', id(t))
結果:
t before update (1, ['a', 'b']) id of t: 48328968 t after update (1, ['x', 'b']) id of t: 48328968
注意:在使用for循環來遍歷一個可更新對象過程當中,增長刪除可更新對象容器的大小會獲得意想不到的結果(不指望的結果)甚至出現異常,其中對於字典,直接報錯;
直接遍歷列表:
1 lst = [1, 1, 0, 2, 0, 0, 8, 3, 0, 2, 5, 0, 2, 6] 2 3 for item in lst: 4 if item == 0: 5 lst.remove(item) 6 print(lst) #結果爲 [1, 1, 2, 8, 3, 2, 5, 0, 2, 6]
遍歷索引:
1 lst = [1, 1, 0, 2, 0, 0, 8, 3, 0, 2, 5, 0, 2, 6] 2 3 for item in range(len(lst)): 4 if lst[item] == 0: 5 del lst[item] 6 print(lst) #結果爲IndexError: list index out of range
字典:
1 dct = {'name':'winter'} 2 for key in dct: 3 if key == 'name': 4 di['gender'] = 'male' 5 print(dct) #結果報錯RuntimeError: dictionary changed size during iteration
for循環來遍歷可變對象時,遍歷順序在最初就肯定了,而在遍歷中若是增長刪除了元素,更新會當即反映到迭代的條目上,會致使當前索引的變化,這樣一是會致使漏刪或多增長元素,二是會致使遍歷超過鏈表的長度。那麼有解決方案嗎?
a) 經過建立一個新的可變對象實現,好比:
b) 經過while循環
1 #經過建立新的copy 2 for item in lst[:]: 3 if item == 0: 4 lst.remove(item) 5 print (lst) 6 #經過while循環 7 while 0 in lst: 8 lst.remove(0) 9 print (lst)
更多的內容,看我關於「迭代器」的博客
3)訪問模型
序列是指容器內的元素按從0開始的索引順序訪問;
字典的元素是一個一個的鍵值對,相似於哈希表;經過獲取鍵,對鍵執行一個哈希操做,根據計算的結果,選擇在數據結構的某個地址中存儲你的值。任何一個值的存儲地址取決於它的鍵;因此字典是無序的,鍵必須是不可變類型(對於元祖,只能是元素不可變的元祖)
總結:
既然字符串,列表,元祖,字典都是對象,那麼任何一種實例化的對象都會有相同的方法,下面讓咱們來依次來認識這些常見的數據類型:
1)建立字符串
兩種方式:
#直接建立字符串引用 s = 'winter' #經過使用str()函數建立 s = str(123)
2)字符串方法
做爲內建對象,字符串類的定義在內建模塊builtins中,經過源碼能夠查看到全部字符串方法;若是你在用pycharm,那麼更加方便,選中str類之後,點擊按鈕"Scroll from Source";就能夠看到全部方法的列表;
針對每一種方法都有詳細的方法文檔;在這裏吐血一一羅列出,有的註釋部分很重要:
1 '''字符串方法''' 2 3 #index,字符串查找,返回第一個匹配到的索引值,沒有就報錯,能夠提供開始,結束位置 4 s = 'xyzabc' 5 ret = s.index('za') #結果爲2 6 ret = s.index('z',2) #結果爲2 7 #rindex,反向查找,結果和index方法獲得的結果同樣 8 #find,也是字符串查找,與index不一樣的是找不到時,不會報錯,而是返回-1,注意bool(-1)爲True 9 10 11 #split,字符串分隔,若是不指定分割符,則默認採用任意形式的空白字符:空格,tab(\t),換行(\n),回車(\r)以及formfeed 12 res = 'a b c d\ne'.split() #結果res爲['a', 'b', 'c', 'd', 'e'] 13 res = 'a:b:c:d'.split(':') #結果爲['a', 'b', 'c', 'd'] 14 #rsplit,從字符串尾部開始分割,沒有設置maxsplit參數時和split的結果同樣 15 #splitlines用於將多行字符串,列表的每一個元素爲一行字符串,默認不帶換行符 16 17 18 #partition,相似於s.split(sep, 1),只是輸出是一個包括了sep的元祖,且沒有默認分隔符sep 19 res = 'a:b:c:d'.partition(':') #結果爲('a', ':', 'b:c:d') 20 #rpartition,從字符串末尾開始執行partition方法 21 res = 'a:b:c:d'.rpartition(':') #結果爲('a:b:c', ':', 'd') 22 23 24 #strip,去掉字符串兩端的字符,若是沒指定字符,則默認去掉任意形式的空白字符:空格,tab(\t),換行(\n),回車(\r)以及formfeed 25 res = 'abcd'.strip('a') #結果res爲'bcd' 26 res = ' abcd \n'.strip() #結果res爲'abcd',能夠經過len(res)驗證 27 #rstrip,去掉右端的字符 28 #lstrip,去掉左端的字符 29 30 31 #join,字符串拼接 32 #format,字符串格式化 33 #解決萬惡的‘+’號,每次'+'操做都會建立一個新的對象,就須要在內存空間開闢一塊新的區域來存儲;因此大量使用'+'會浪費內存空間 34 #解決方法1,使用join 35 #join,字符串拼接 36 li = ['a', 'b'] 37 res = ':'.join(li) #使用字符串來拼接一個可迭代對象的全部元素,結果res爲'a:b' 38 #解決方法2,使用format,有兩種方式: 39 #方式1,其中{0}能夠簡寫爲{}: 40 res = 'my name is {0}, my age is {1}'.format('winter',30) #{0},{1}爲實參的索引 41 #方式2: 42 res = 'my name is {name}, my age is {age}'.format(name='winter', age=30) 43 #或者直接下面這樣 44 userInfo = {'name':'winter','age':30} 45 res = 'my name is {name}, my age is {age}'.format(**userInfo) 46 #或者用format_map(python3.x新增) 47 userInfo = {'name':'winter','age':30} 48 res = 'my name is {name}, my age is {age}'.format_map(userInfo) 49 #解決方法3,除了format之外,還可使用佔位符%,看上去沒那麼高級,一樣有兩種方式 50 #方式1 51 res = 'my name is %s, my age is %s'%('winter',30) 52 #方式2 53 userInfo = {'name':'winter','age':30} 54 res = 'my name is %(name)s, my age is %(age)s'%userInfo 55 #結果爲'my name is winter, my age is 30' 56 57 #replace,字符串替換,由於字符串爲不可變數據類型,因此原字符串不會改變,改變的字符串以返回值形式返回 58 s = '123xyz' 59 ret = s.replace('1','a') #結果s不變,ret爲'a23xyz' 60 ret = s.replace('12', 'ab') #結果爲'ab3xyz' 61 62 63 #maketrans和translate,一對好基友;用於字符串替換和刪除,很好用,python2.x與python3.x語法不一樣 64 #maketrans用於建立翻譯表,translate用於翻譯表 65 s = 'abccba' 66 trans_table = str.maketrans('ab','12','c') 67 res = s.translate(trans_table) #結果爲'1221',注意與replace的區別 68 69 70 s.count(strings) #count,統計字符串中字符的數目 71 72 73 s.encode() #以某種編碼格式編碼,好比utf-8 74 75 76 #大小寫切換類方法 77 s = 'aBcD' 78 res = s.capitalize() #首字母大寫,結果res爲'Abc' 79 res = s.lower() #全部字母小寫,結果res爲'abcd',注意僅對ASCII編碼的字母有效 80 res = s.casefold() #全部字母小寫,結果res爲'abcd',注意對unicode編碼的字母有效 81 res = s.upper() #全部字母大寫,結果res爲'ABCD' 82 res = s.swapcase() #字符串中大小寫互換,結果res爲'AbCd' 83 s = 'aB cD,ef' 84 res = s.title() #每一個單詞首字母大寫,結果res爲'Ab Cd,Ef',其判斷「單詞」的依據則是基於空格和標點 85 86 87 #字符串判斷類方法,如下方法除了isspace()之外,對於空字符串都返回False 88 s.isalpha() #判斷字符串是否全爲字母,等價於^[a-zA-Z]+$ 89 s.islower() #判斷字符串中字母部分是否全爲小寫字母,能夠有其餘字符,可是至少要有一個小寫字母,等價於[a-z]+ 90 s.isupper() #判斷字符串中字母部分是否全爲小寫字母,能夠有其餘字符,可是至少要有一個小寫字母,等價於[A-Z]+ 91 s.istitle() #判斷字符串首字母是否全爲大寫,能夠有其餘字符 92 s.isnumeric() #判斷字符串是否全爲數字(包括unicode數字,羅馬數字,漢字數字) 93 s.isdigit() #也是判斷字符串是否全爲數字(包括unicode數字,羅馬數字) 94 s.isdecimal() #判斷字符串是否全爲十進制數字(包括unicode數字) 95 s.isalnum() #判斷字符串是否爲數字與字母的組合,等價於^[0-9a-zA-Z]+$ 96 s.isidentifier() #判斷標識符是否合法 97 s.isspace() #判斷是否爲任意形式的空白字符:空格,tab(\t),換行(\n),回車(\r)以及formfeed 98 s.isprintable() #判斷字符串是否可打印 99 s.startswith(strings) #判斷字符串是否以某個字符串開頭 100 s.endswith(stings) #判斷字符串是否以某個字符串結尾 101 102 103 #center,ljust,rjust,zfill字符串排版,填充 104 s = '123' 105 res = s.center(50,'-') #center是字符串居中,結果爲-----------------------123------------------------ 106 #ljust爲居左,rjust爲居右,zfill使用0來左填充 107 108 109 s.expandtabs() #expandtables,用於將字符串中的tab字符轉換爲空格,若是沒有指定tabsize,默認爲8個
1)建立列表
三種方式:
a = ['a','b','c'] #直接建立 b = list('abc') #經過建立list實例建立,結果爲['a','b','c'] c = [x for x in range(4)] #列表推導式,結果爲[1,2,3,4]
注意,第二種方法,咱們來看一下它的構造函數:
def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) -> new list initialized from iterable's items # (copied from class doc) """ pass
接收的參數是一個可迭代對象,即擁有__iter__方法的對象,元祖,集合和字典的構造函數的參數都是可迭代對象;
第三種方法稱爲列表推導式,語法爲
[expr for iter_var in iterable] #迭代iterable裏全部內容,每一次迭代,都把iterable裏相應內容放到iter_var中,再在表達式中應用該iter_var的內容,最後用表達式的計算值生成一個列表。
擴展形式
[expr for iter_var in iterable if cond_expr] #加入了判斷語句
2)列表方法
與查看字符串方法相似,能夠經過源碼查看,這裏也一一羅列出:
1 #增,append,insert,extend 2 #追加append 3 li = ['a','b','c'] 4 li.append('d') #結果li = ['a','b','c','d'],list是可變類型,因此li元素的增長刪除會影響源列表 5 #插入insert 6 li = ['a','b','c'] 7 li.insert(1,'d') #結果li = ['a','d','b','c'] 8 #extend,將一個可迭代對象增長追加到列表裏 9 li = ['a','b','c'] 10 li.extend(['x','y']) #結果li = ['a','b','c','x','y'] 11 li.extend(('m','n')) #結果li = ['a', 'b', 'c', 'x', 'y', 'm', 'n'] 12 13 14 #刪,remove,pop,clear 15 #remove,刪除第一個與參數值相等的元素,刪除不存在的元素報ValueError 16 li = ['a','b','c','a'] 17 li.remove('a') #結果li = ['b', 'c', 'a'] 18 #pop,參數爲索引值,彈出對應索引值的元素,返回值爲彈出的元素,默認爲彈出最後一個元素,索引不存在報IndexError 19 li = ['a','b','c','a'] 20 res = li.pop(1) #結果res = ‘b',li = ['a', 'c', 'a'] 21 #clear,清空列表,可是列表對象依然存在,沒有被回收 22 li = ['a','b','c','a'] 23 li.clear() #結果li = [] 24 25 26 #查,count,index 27 #count,統計列表內元素個數 28 li = ['a','b','c','a'] 29 res = li.count('a') #結果res = 2 30 #index,查找元素對應的索引值,不存在報ValueError 31 li = ['a', 'b', 'c', 'a'] 32 res = li.index('a') #結果res = 0 33 34 35 #reverse,反轉 36 li = ['a','b','c'] 37 li.reverse() #結果li = ['c','b','a'] 38 39 40 #sort,根據key排序,默認從小到大,一樣能夠實現反轉 41 li = ['c','b','a'] 42 li.reverse() #結果li = ['a', 'b', 'c'] 43 li = [[4,5,6], [7,2,5], [6,1,8]] 44 li.sort(key=lambda x:x[1]) #結果li = [[6, 1, 8], [7, 2, 5], [4, 5, 6]],根據li元素的第二個值進行排序 45 46 47 #copy,淺複製,注意與賦值和深複製的區別 48 li = [[1,2,3], 'a'] 49 li_new = li.copy() 50 li_new[0][0] = 4 #li_new = [[4, 2, 3], 'a'],可是li也發生了變化li = [[4, 2, 3], 'a'] 51 li_new[1] = 'b' #li_new = [[1, 2, 3], 'b'],可是li不變,[[1, 2, 3], 'a']
1)建立元祖
兩種方式:
t = (1,2,3) #直接建立 t = tuple([1,2,3]) #經過tuple函數建立,參數爲可迭代對象,結果爲(1,2,3)
2)元祖方法
和字符串,同樣,經過源碼查看,這裏一一列出:
#count,統計元祖內元素個數 t = (1,2,3,1) res = t.count(1) #結果res = 2 #index,查找元素對應的索引值,不存在報ValueError t = (1,2,3,1) res = t.index(1) #結果res = 0
元祖的方法不多,並且元祖對象是不可變的,元組不可變的好處。保證數據的安全,好比咱們傳數據到一個不熟悉的方法或者數據接口, 確保方法或者接口不會改變咱們的數據從而致使程序問題。
注意,由於()處理用於定義元祖,還能夠做爲分組操做符,好比if ( a> b) and (a < c)中的(),且python遇到()包裹的單一元素,首先會認爲()是分組操做符,而不是元組的分界符,因此通常咱們對於元組,會在)以前再加入一個,
a = (1,2,3,)
3) 列表vs元組
使用不可變類型變量的狀況:若是你在維護一些敏感數據,而且須要把這些數據傳遞給一個不瞭解的函數(或一個根本不是你寫的API),做爲一個只負責一個軟件某一部分的工程師,若是你但願你的數據不要被調用的函數篡改,考慮使用不可變類型的變量;
須要可變類型參數的狀況:管理動態數據集合時,你須要先建立,而後漸漸地不按期地添加,或者有時須要移除一些單個的元素,這時必須使用可變類型對象;將可變類型變量做爲參數傳入函數,在函數內能夠對參數進行修改,可變類型變量也會相應修改;
字符串,列表,元祖統稱序列,是能夠順序訪問的;他們的關係和共有特性以下:
1)相互轉換
2)序列操做符
a)根據索引取值,能夠正向索引,也能夠反向索引
li = [1,2,3,4] res = li[0] #結果爲1 res = li[-1] #結果爲4 li[1] = ['a'] #結果li = [1,'a',3,4]
b)切片
seq[ind1:ind2:step],原則是顧頭不顧尾
li = [1,2,3,4] res = li[:] #結果爲[1, 2, 3, 4] res = li[1:] #結果爲[2,3,4] res = li[:-2] #結果爲[1,2] res = li[1:3] #結果爲[2,3] res = li[::2] #取索引值爲偶數的元素[1,3],對應的li[1::2]爲取索引值爲基數的元素 res = li[::-1] #步進爲-1,結果爲[4, 3, 2, 1] res = li[-1:-3:-1] #結果爲[4,3] res = li[3:1:-1] #結果爲[4,3] res = li[1:1] #結果爲[] li[1:2] = ['a','b'] #結果li = [1, 'a', 'b', 3, 4] li[1:1] = [9] #結果li = [1, 9, 'a', 'b', 3, 4],至關於插入
注意序列不能讀寫不存在的索引值,可是切片能夠
li = [1,2,3] li[9] #報IndexError li[1:9] #結果爲[2,3] li[8:9] = [9] #結果爲[1,2,3,9]
c)重複操做符*
[1]*3 #結果爲[1,1,1] [1,2,3]*3 #結果爲[1, 2, 3, 1, 2, 3, 1, 2, 3]
d)鏈接操做符+
[1,2,3]+[4,5,6] 結果爲 [1, 2, 3, 4, 5, 6]
e)成員關係操做符in, no in
li = [1,2,3] 1 in li #結果爲True 4 not in li #結果爲True
f)刪除序列對象,元素關鍵字del
l = [1,2,3] del l[0] #結果l爲[2,3] del l #結果訪問l後,報NameError
3)可操做內建函數
1 #enumerate,枚舉 2 li = ['a','b','c'] 3 for item in enumerate(li): 4 print(item) 5 ''' 6 結果爲: 7 (0, 'a') 8 (1, 'b') 9 (2, 'c') 10 ''' 11 for item in enumerate(li,5): 12 print(item) 13 ''' 14 結果爲: 15 (5, 'a') 16 (6, 'b') 17 (7, 'c') 18 ''' 19 20 21 #len 22 len([1,2,3]) #結果爲3 23 len([[1,2],[3,4]]) #結果爲2 24 25 26 #max, min 27 max([1,2,3]) #結果爲3 28 min([1,2,3]) #結果爲1 29 30 31 #sorted,注意與list.sort()方法的區別 32 li = [[1,4],[2,3],[3,2]] 33 res = sorted(li, key=lambda x:x[1]) #結果res爲[[3, 2], [2, 3], [1, 4]] 34 35 36 #zip,和倒置有點像 37 l1 = [1,2,3] 38 l2 = [1,2,3] 39 for item in zip(l1,l2): 40 print(item) 41 ''' 42 結果爲: 43 (1, 1) 44 (2, 2) 45 (3, 3) 46 ''' 47 l1 = [1,2,3] 48 l2 = [1,2] 49 for item in zip(l1,l2): 50 print(item) 51 ''' 52 結果爲: 53 (1, 1) 54 (2, 2) 55 '''
字典是Python語言中惟一的映射類型。映射對象類型裏哈希值(鍵,key)和指向的對象(值,value)是一對多的關係;
特性:
a)字典是無序的可變的容器類型
b)字典的鍵是惟一的,且必須是不可變類型,若是是元組,要求元組的元素也不可變(由於值的內存地址與鍵的哈希值有關)
1)建立字典
1 #直接建立 2 d = { 3 'name':'winter', 4 'age':18, 5 'hobbies':['basketball','football'] 6 } 7 8 #經過dict函數建立,參數爲可迭代對象,字典或列表組成的任意形式;[(),()]或((),())或([],[])或[[],[]]或使用dict(zip()) 9 d = dict([('name', 'winter'), 10 ('age',18), 11 ('hobbies',['basketball','football'])]) #結果爲{'age': 18, 'hobbies': ['basketball', 'football'], 'name': 'winter'} 12 13 #經過字典推導式 14 d = {x:y for x, y in zip(['name','age','hobbies'],['winter',18,['basketball','football']])} #結果爲{'age': 18, 'hobbies': ['basketball', 'football'], 'name': 'winter'} 15 d = {x:y for x, y in zip(['name','age','hobbies'],['winter',18,['basketball','football']]) if x=='name'}#結果爲{'name': 'winter'} 16 17 #經過字典方法fromkeys()建立一個含有默認值的字典,fromkeys方法是dict類的靜態方法 18 d = dict.fromkeys(['name','gender'], 'unknown') #結果{'gender': 'unknown', 'name': 'unknown'}
2)字典方法
1 #增 2 d = { 3 'name':'winter', 4 'age':18, 5 'hobbies':['basketball','football'] 6 } 7 #update,相似於list.extend(),將鍵值對參數加到字典中,對於已存在的更新 8 d.update({'gender':'male'}) #結果爲:{'age': 18, 'gender': 'male', 'hobbies': ['basketball', 'football'], 'name': 'winter'} 9 10 11 #刪 12 d = { 13 'name':'winter', 14 'age':18, 15 'hobbies':['basketball','football'] 16 } 17 #popitem,隨機刪除一組鍵值對,並返回該組鍵值對 18 res = d.popitem() #結果爲res = ('hobbies', ['basketball', 'football']), d爲{'age': 18, 'name': 'winter'} 19 #pop,刪除指定鍵的鍵值對,返回對應的值 20 res = d.pop('name') #結果爲res = 'winter', d爲{'age': 18} 21 #clear,清空字典,字典對象還在,沒有被回收 22 d.clear() #結果d爲{} 23 #del關鍵字,能夠用於刪除字典對象和元素 24 d = { 25 'name':'winter', 26 'age':18, 27 'hobbies':['basketball','football'] 28 } 29 del d['hobbies'] #結果爲 {'age': 18, 'name': 'winter'} 30 del d #結果訪問d,報NameError 31 32 33 #查 34 d = { 35 'name':'winter', 36 'age':18, 37 'hobbies':['basketball','football'] 38 } 39 #get,根據鍵取對應的值,沒有對應的鍵,就返回第二個參數,默認爲None 40 res = d.get('name', 'not found') #res = ’winter' 41 res = d.get('gender', 'not found') #res = 'not found' 42 #setdefault,根據鍵取對應的值,若是不存在對應的鍵,就增長鍵,值爲第二個參數,默認爲None 43 res = d.setdefault('name','winter') #res = ’winter' 44 res = d.setdefault('gender','male') #res = 'male',d變爲{'age': 18, 'gender': 'male', 'hobbies': ['basketball', 'football'], 'name': 'winter'} 45 #keys,得到全部的鍵 46 res = d.keys() #res = dict_keys(['name', 'age', 'hobbies', 'gender']) 47 #values,得到全部的值 48 res = d.values() #res = dict_values(['winter', 18, ['basketball', 'football'], 'male']) 49 #items,得到全部的鍵值對 50 res = d.items() #res = dict_items([('name', 'winter'), ('age', 18), ('hobbies', ['basketball', 'football']), ('gender', 'male')]) 51 52 53 #copy方法, 和list.copy同樣,是淺複製
3)字典元素的訪問
1 dic = {'name':'winter', 'age':18} 2 ''' 3 下面3中方式的結果都爲 4 name winter 5 age 18 6 ''' 7 #方式1 8 for item in dic: 9 print(item, dic[item]) 10 #方式2 11 for item in dic.keys(): 12 print(item, dic[item]) 13 #方式3 14 for k,v in dic.items(): 15 print(k, v)
推薦方式1),採用了迭代器來訪問;
4)字典vs列表
a) 字典:
b) 列表:
這是高一數學的內容了
集合對象是一組無序排列的值,分爲可變集合(set)和不可變集合(frozenset);可變集合不可hash;不可變集合能夠hash,便可以做爲字典的鍵;
特性:
a) 無序性,沒法經過索引訪問
b) 互異性,元素不可重複,即有去重的功能
1) 建立集合
1 #直接建立可變集合 2 s1 = {1,2,3,3} #結果,python3.x爲{1,2,3},python2.x爲set([1, 2, 3]),由於集合不容許有重複元素,因此只有一個3元素 3 #經過函數建立 4 s2 = set([1,2,3,4]) #建立可變集合{1, 2, 3, 4} 5 s3 = frozenset([1,2,3,3]) #建立不可變集合,結果爲frozenset({1, 2, 3})
2) 集合方法和操做符
這裏直接用一張表:
這裏只列出可變集合的方法示例,由於不可變集合的方法可變集合都有
a) 單個集合的操做
1 #集合 2 3 #直接建立可變集合 4 s1 = {1,2,3,3} #結果,python3.x爲{1,2,3},python2.x爲set([1, 2, 3]),由於集合不容許有重複元素,因此只有一個3元素 5 #經過函數建立 6 s2 = set([1,2,3,4]) #建立可變集合{1, 2, 3, 4} 7 s3 = frozenset([1,2,3,3]) #建立不可變集合,結果爲frozenset({1, 2, 3}) 8 9 10 s1 = {1,2,3} 11 12 #增 13 s1.add('456') #結果s1 = {1, 2, 3, '456'} 14 s1.update('789') #結果s1 = {1, 2, 3, '7', '8', '9', '456'} 15 16 #刪 17 res = s1.pop() #結果res = 1,s1 = {2, 3, '7', '8', '9', '456'} 18 s1.remove(3) #結果s1 = {2, '7', '8', '9', '456'} 19 s1.discard(4) #結果s1 = {2, '7', '8', '9', '456'},同時不報Error 20 s1.clear() #結果s1 = set() 21 22 #淺複製 23 s1 = {1,2,3} 24 s2 = s1.copy() #結果s2 = {1,2,3}
b) 集合之間的操做,集合的主要功能
1 s0 = {1,2} 2 s1 = {1,2,3,4} 3 s2 = {3,4,5,6} 4 5 #子集,超級判斷 6 res = s0.issubset(s1) #結果res = True 7 res = s1.issubset(s2) #結果res = False 8 res = s1.issuperset(s0) #結果res = True 9 res = s0 <= s1 #結果res = True 10 11 #並集 12 res = s1.union(s2) #結果res = {1, 2, 3, 4, 5, 6} 13 res = s1 | s2 #結果res = {1, 2, 3, 4, 5, 6} 14 15 #交集 16 res = s1.intersection(s2) #結果爲{3, 4} 17 res = s1 & s2 #結果爲{3, 4} 18 19 #差集 20 res = s2.difference(s1) #結果res = {5, 6} 21 res = s2 - s1 #結果res = {5, 6} 22 res = s1.difference(s2) #結果res = {1, 2} 23 24 #對稱差集 25 res = s2.symmetric_difference(s1) #結果爲{1, 2, 5, 6} 26 res = s2 ^ s1 #結果爲{1, 2, 5, 6} 27 28 #在原集合基礎上修改原集合 29 s1.intersection_update(s2) 30 s1.difference_update(s2) 31 s1.symmetric_difference_update(s2)
用一張圖來解釋一下:
2017年9月20日更新
1. 推導式
有列表推導式,字典推導式,集合推導式
1)列表推導式
1 li_1 = [item for item in range(5)] #結果li_1 = [0, 1, 2, 3, 4] 2 3 li_2 = [item for item in range(5) if item > 2] #結果li_2 = [3, 4] 4 5 li_3 = [item if item>2 else 0 for item in range(5)] #結果li_3 = [0, 0, 0, 3, 4] 6 7 li_4 = [(x,y) for x in range(3) for y in range(3,6)] #結果li_4 = [(0, 3), (0, 4), (0, 5), (1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5)] 8 9 li_raw = [['winter', 'tom', 'lily'], ['winter']] 10 li_5 = [item for li in li_raw for item in li if item.count('l') > 1] #結果li_5 = ['lily']
2)集合推導式
和列表推導式同樣,只要把[]換成{}便可
3)字典推導式
和列表推導式同樣,只是沒有if...else...形式
4)有元祖推導式嗎?
(x for x in range(5))就是元祖推導式呢?
答案是:不是的
(x for x in range(5)獲得的是一個生成器
>>>a = (x for x in range(10)) >>>a >>><generator object <genexpr> at 0x0000000004B09F68>>>>a.__next__()>>>0>>>a.__next__()>>>1