關于內存分配問題
1 ##字符串新定義則開辟新的一塊內存空間 2 >>> str1 = 'hoho' 3 >>> str2 = str1 4 >>> id(str1),id(str2) #查看內存對象地址,觀察內存地址,即str2新開辟了內存空間 5 (140297199501752, 140297199501752) #這里看到是一樣的是由于python的一個內部機制導致的,如果字符串足夠大的話就會是不一樣的,不用糾結 6 >>> str2 = 'heihei' 7 >>> str1 8 'hoho' 9 >>> str210 'heihei'11 >>> id(str1),id(str2) #看,內存地址是不是變了12 (140297199501752, 140297214622552) 13 14 ##非字符器,如列表,元組,字典賦值定義后其實只是把新的變量名(可以理解為標簽)指向同一內存地址,以字典為例,如下所示15 >>> dic1 = {'name':'hoho'}16 >>> dic2 = dic117 >>> id(dic1),id(dic2)18 (140297199190088, 140297199190088)19 >>> dic1 = {'name':'hoho'}20 >>> dic2 = dic121 >>> id(dic1),id(dic2) #查看內存對象地址,發現是一樣的,故修改dic2事實上dic1也跟著修改了22 (140297199191752, 140297199191752)23 >>> dic2['name'] = 'heihei'24 >>> dic225 {'name': 'heihei'}26 >>> dic127 {'name': 'heihei'}
列表,元組及字典的復制問題(淺復制與深復制 copy模塊的使用)
1、列表及元組可使用切片實現淺復制,也可使用 copy模塊使用淺復制(包括字典)
2、使用copy.deepcopy() 實例深復制
1 >>> import copy 2 >>> list1 = [1,2] 3 >>> list2 = list1 4 >>> list2[0] = 2 #list2改了,list1跟著變了 5 >>> list1 6 [2, 2] 7 >>> list3 = list1[:] #淺復雜,利用數組切片做淺復制 8 >>> list3 = copy.copy(list1) 9 >>> id(list1),id(list2),id(list3) #這里就可看到地址空間是不一樣的10 (140297199250696, 140297199250696, 140297199247560)11 >>> 12 >>> list4 = [1,[2]] ##復雜結構必須用深復制13 >>> list5 = list4[:]14 >>> list515 [1, [2]]16 >>> list5[1][0] = 617 >>> list418 [1, [6]] #從這里可以看到內層的列表事實是沒復制的,list4也跟著變了19 >>> list6 = copy.deepcopy(list4) #這里使用深復制20 >>> list6[1][0] = 821 >>> list622 [1, [8]]23 >>> list424 [1, [6]] #這里就可以看出已經復制的了
常用內置函數
python內置函數是非常多的,記住常用的就行了,但會知道怎么查看有哪些內置函數,函數的幫助
正常情況下分3步走
整形

1 >>> s,y = divmod(7,3) ## divmod 返回數據,值為(商,余數),可用于分頁2 >>> s,y3 (2, 1)4 >>> a = -25 >>> abs(-2) #abs取絕對值6 27 >>> len(str(-2)) #取速度長度8 2
浮點

1 >>> a = 7.02 >>> divmod(a,3)3 (2.0, 1.0)4 >>> a = 7.2355 >>> a.__round__(2) #四舍五入6 7.247 >>> a.__trunc__() #取整8 7
字符串

1 >>> str1 = 'this is a string' 2 >>> 'is' in str1 #成員判斷 3 True 4 >>> str1[1:3] # 切片操作與索引 5 'hi' 6 >>> len(str1) #長度 7 16 8 >>> str1.find('is') #查找字符串,返回索引值 9 210 >>> str1.find('is',3,9)11 512 >>> str1.find('iss') #沒有找到返回-1 ,如是index則會報錯13 -114 >>> str1.index('is',3)15 516 >>> str1.index('iss')17 Traceback (most recent call last):18 File "", line 1, in 19 ValueError: substring not found20 >>> str1 = ' aaa'21 >>> str1.strip() 去空白,換行,回車22 'aaa'23 >>> str1.lstrip()24 'aaa'25 >>> str1.rstrip()26 ' aaa'27 >>> str1 = 'duiqi' #對齊操作28 >>> str1.ljust(20)29 'duiqi '30 >>> str1.ljust(20,'*')31 'duiqi***************'32 >>> str1.rjust(20,'*')33 '***************duiqi'34 >>> str1.center(20,'*')35 '*******duiqi********'36 >>> str1 = 'this is a string'37 >>> str1.split() ##分割操作38 ['this', 'is', 'a', 'string']39 >>> str1.splitlines()40 ['this is a string']41 >>> list1 = [1,2,3]42 >>> '->'.join([str(i) for i in list1]) #連接操作43 '1->2->3'44 >>> str145 'this is a string'46 >>> str1.count('is') #計數47 248 >>> str1.replace('is','os') #替換49 'thos os a string'50 >>> str1.replace('is','os',1) #替換,只替換1個51 'thos is a string'52 53 str1.startswith('sub') #以什么開頭54 str1.endswith('sub') #以什么結尾55 str1.lower() #轉為小寫56 str1.upper() #轉為大寫
列表與元組(元組不可修改)

1 >>> lst1 = ['a'] 2 >>> lst1.append('b') #新增 3 >>> lst1 4 ['a', 'b'] 5 >>> lst2 = ['c','d'] 6 >>> lst1.extend(lst2) #擴展新增 7 >>> lst1 8 ['a', 'b', 'c', 'd'] 9 >>> lst1.insert(0,'z') #插入10 >>> lst111 ['z', 'a', 'b', 'c', 'd']12 >>> lst1.pop() #去除末尾13 'd'14 >>> lst115 ['z', 'a', 'b', 'c']16 >>> lst1.remove('z') #刪除指定元素17 >>> lst118 ['a', 'b', 'c']19 >>> lst1 = ['a', 'b', 'c', 'd']20 >>> lst2 = lst1.copy() # 淺復制 python3才有21 >>> lst2 = lst1.copy()22 >>> lst223 ['a', 'b', 'c', 'd']24 >>> lst2.clear() #清空列表25 >>> lst226 []27 >>> del lst2 #刪除列表28 >>> lst129 ['d', 'c', 'b', 'a']30 >>> lst1.sort() #排序31 >>> lst132 ['a', 'b', 'c', 'd']33 >>> lst1.append('a')34 >>> lst1.count('a') #計數35 236 >>> lst137 ['a', 'b', 'c', 'd', 'a']38 >>> len(lst1) #長度39 540 >>> lst1.index('a') #索引41 042 >>> lst1.index('a',1) #索引43 4
字典

1 >>> dic1 = {'key1' : 'a','key2' : 'b'} 2 >>> dic1.get('key1') #取字典值,沒取到默認返回None,也可指定 3 'a' 4 >>> dic1.get('key3') 5 >>> dic1.items() 6 dict_items([('key2', 'b'), ('key1', 'a')]) #返回元組列表 7 >>> list(dic1.items()) 8 [('key2', 'b'), ('key1', 'a')] 9 >>> dic1.keys() #返回keys列表10 dict_keys(['key2', 'key1'])11 >>> dic1.values() #返回值列表12 dict_values(['b', 'a'])13 >>> dic2 = dic1.copy() #淺復制14 >>> dic215 {'key2': 'b', 'key1': 'a'}16 >>> dic1['key3'] = 'c' #賦值(修改)17 >>> dic118 {'key2': 'b', 'key1': 'a', 'key3': 'c'}19 >>> dic1.pop('key1') #刪除指定的key20 'a'21 >>> dic122 {'key2': 'b', 'key3': 'c'}23 >>> dic1.get('key1','a') #取值,沒有返回'a'24 'a'25 >>> dic126 {'key2': 'b', 'key3': 'c'}27 >>> dic1.setdefault('key1','a') #設置默認(貌似沒什么用)28 'a'29 >>> dic130 {'key2': 'b', 'key1': 'a', 'key3': 'c'}31 >>> dic3 = {'name':'update'}32 >>> dic1.update(dic3) #更新33 >>> dic1 34 {'key2': 'b', 'name': 'update', 'key1': 'a', 'key3': 'c'}35 >>> del dic3 #刪除36 >>> dic137 {'key2': 'b', 'name': 'update', 'key1': 'a', 'key3': 'c'}38 >>> len(dic1) #長度39 4
新聞熱點
疑難解答