Python 學習筆記(八)Python列表(二),python學習筆記
列表函數
追加和擴充
list.append() 在列表末尾追加新的對象
1 >>> dir(list) #dir 查看列表的函數 2 ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] 3 >>> help(list.append) #help 查看 list.append() 函數的詳細內容 4 Help on method_descriptor: 5 6 append(...) 7 L.append(object) -- append object to end #將對象追加到末尾 8 9 >>> a =[2,6]10 >>> a.append(50) #將對象50追加到列表a中11 >>> a12 [2, 6, 50]13 >>> a.append("python book") 14 >>> a15 [2, 6, 50, 'python book']16 >>> a17 [2, 6, 50, 'python book', ['baidu', 'weibo']]18 >>> b =[1]19 >>> id(b) #id 傳回值給出在記憶體中的空間20 60126664L21 >>> b.append(5)22 >>> b23 [1, 5]24 >>> b25 [1, 5]26 >>> id(b) #追加5 之後,id傳回值 一樣27 60126664L28 >>> b.append("zhangsan")29 >>> id(b)30 60126664L31 >>> b32 [1, 5, 'zhangsan']33 >>>
註:列表在被修改的時候,不是建立了一個新的列表,而是修改了原來的列表,這種修改稱為原地修改
extend()在列表末尾一次性追加另一個序列中的多個值(用新列表擴充原來的列表)
1 >>> help(list.extend) #help查看list.extend函數的詳細內容 2 Help on method_descriptor: 3 4 extend(...) 5 L.extend(iterable) -- extend list by appending elements from the iterable #把可迭代對象中的元素追加到列表中 6 7 >>> a=[1,2,3] #可迭代對象 8 >>> b=[4,5,6] #可迭代對象 9 >>> a.extend(b) 10 >>> a #b列表中的元素被一個個追加到a列表中11 [1, 2, 3, 4, 5, 6] 12 >>> a.extend("python")13 >>> a14 [1, 2, 3, 4, 5, 6, 'p', 'y', 't', 'h', 'o', 'n']15 >>> alst =[1,2]20 >>> hasattr(alst,'__iter__') #判斷是否可迭代,是返回true ,否則false21 True22 >>> hasattr("python",'__iter__') #字串不可迭代,此處是將字串一個的字元拆出追加23 False24 >>> a =[1,2]25 >>> a.append([4,5])26 >>> a27 [1, 2, [4, 5]]28 >>> a.extend([4,5]) 29 >>> a30 [1, 2, [4, 5], 4, 5]31 >>> b =[9,8]32 >>> a.append(b[0])33 >>> a.append(b[1])34 >>> a35 [1, 2, [4, 5], 4, 5, 9, 8]36 >>>
註:append 與extend 區別就是,extend 將一個個的元素拆分追加,append是整體追加
其他函數
count() 統計某個元素在列表中出現的次數
1 >>> help(list.count) 2 Help on method_descriptor: 3 4 count(...) 5 L.count(value) -> integer -- return number of occurrences of value #返回出現的次數 6 7 >>> a =[1,1,1,2,2,2,3,3,3] 8 >>> a.count(1) #1這個元素在列表中出現3次 9 310 >>> a.count(2) #2這個元素在列表中出現3次11 312 >>> a.count("a") #a在列表中沒有13 0
index() 從列表中找出某個值第一個匹配項的索引位置
1 >>> help(list.index) 2 Help on method_descriptor: 3 4 index(...) 5 L.index(value, [start, [stop]]) -> integer -- return first index of value. #參數中的value值在列表中第一次出現的索引位置 6 Raises ValueError if the value is not present. 7 8 >>> a.index(1) 9 010 >>> a.index(2)11 312 >>>
insert() 將對象插入列表
1 >>> help(list.insert) 2 Help on method_descriptor: 3 4 insert(...) 5 L.insert(index, object) -- insert object before index #把對象插入到索引所對應的元素的前面 6 7 >>> a =["python","web"] 8 >>> a.insert(1,"aaa") #在索引是1的元素的前面插入一個字串aa 9 >>> a10 ['python', 'aaa', 'web']11 >>> a.insert(0,"like") #在最前面插入一個字串like12 >>> a13 ['like', 'python', 'aaa', 'web']14 >>>
註:insert為原地修改,沒有建立一個列表
pop() 移除列表中的一個元素(預設最後一個元素),並且返回該元素的值
1 >>> help(list.pop) 2 Help on method_descriptor: 3 4 pop(...) 5 L.pop([index]) -> item -- remove and return item at index (default last). #刪除索引所對應的元素,並它作為傳回值返回 (預設刪除最後一個元素) 6 Raises IndexError if list is empty or index is out of range. #不能刪除為空白的或者超出索引範圍的元素,否則索引錯誤 7 8 >>> a 9 ['like', 'python', 'aaa', 'web']10 >>> a.pop(1)11 'python'12 >>> a13 ['like', 'aaa', 'web']14 >>>
remove() 移除列表中某個值的第一個匹配項
1 >>> help(list.remove) 2 Help on method_descriptor: 3 4 remove(...) 5 L.remove(value) -- remove first occurrence of value. #移除某個值得第一個匹配項 6 Raises ValueError if the value is not present. 7 8 >>> a =["test","test","demo"] 9 >>> a.remove("test")10 >>> a11 ['test', 'demo']12 >>> a.remove("aa")13 Traceback (most recent call last):14 File "<stdin>", line 1, in <module>15 ValueError: list.remove(x): x not in list16 >>>
reverse() 反向列表中元素
1 >>> help(list.reverse) 2 Help on method_descriptor: 3 4 reverse(...) 5 L.reverse() -- reverse *IN PLACE* 6 7 >>> a =[1,2,3,4,5] 8 >>> a.reverse() 9 >>> a10 [5, 4, 3, 2, 1]11 >>>
sort() 對原列表進行排序
1 >>> help(list.sort) 2 Help on method_descriptor: 3 4 sort(...) 5 L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; 6 cmp(x, y) -> -1, 0, 1 7 8 >>> a =[5,9,3,1] 9 >>> a.sort() #從小到大排列10 >>> a11 [1, 3, 5, 9]12 >>> b=[9,3,8,6]13 >>> b.sort(reverse=True) #從大到小排列14 >>> b15 [9, 8, 6, 3]16 >>>