python--列表操作

來源:互聯網
上載者:User

一,列表操作思維導圖



二,列表操作詳細講解

1,定義與初始化

lst = list( )                                 #使用list定義一個空列表

lst = [ ]                                       #使用[ ]定義一個空列表

lst = [1,2,3]                               #使用[ ]定義並初始化列表

lst = list(range(10))                # 使用list函數把可 迭代對象轉化為列表


通常在定義列表的時候使用  [ ],在轉化為可迭代對象為列表的時候,使用list函數


2,列表元素的訪問,通過下標(也叫索引)進行訪問

>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> lst[0]                   
0
>>> lst[10]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> lst[-1]
9
>>> lst[-11]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

當索引超出範圍的時候會報IndexError,正索引和負索引都會報


    通過值尋找索引,list.index() 包含三個參數,value,start,stop ,根據需要來選擇

index(...)
    L.index(value, [start, [stop]]) -> integer -- return first index of value.

  >>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst.index(5)
5

start參數指定從哪個方向開始尋找,stop參數指定從哪個索引結束,並且不包含該索引,當值不存在的時候,拋出ValueError

 >>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]


>>> lst.index(5,1,4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 5 is not in list
>>> lst.index(5,1,6)
5
>>> lst.index(5,1,5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 5 is not in list
>>>

start和stop 可以為負數,但是總是從左往右尋找,stop必須必start大,否則報錯

>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst.index(5,-4,-1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 5 is not in list
>>> lst.index(5,-6,-1)
5


3,list.count()  返回某個元素在列表裡的個數

>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst.count(9)
1


4,修改列表裡的元素

>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[3]=5
>>> lst
[0, 1, 2, 5, 4, 5, 6, 7, 8, 9]

修改元素只有這一種方法


4,增加元素到列表裡

增加元素,無法使用索引操作

list.append(x)         增加一個元素到列表結尾

list.insert(2,11)       在索引為2的元素之前增加一個元素11

>>> lst
[0, 1, 2, 5, 4, 5, 6, 7, 8, 9]
>>> lst.append(10)
>>> lst
[0, 1, 2, 5, 4, 5, 6, 7, 8, 9, 10]
>>> lst.insert(9,100)
>>> lst
[0, 1, 2, 5, 4, 5, 6, 7, 8, 100, 9, 10]


lst.extend() 可以增加可迭代對象到列表的結尾,extend無法增加單個元素,必須是可迭代對象

>>> lst = [1,2,3,4,5]
>>> lst.extend(range(3))
>>> lst
[1, 2, 3, 4, 5, 0, 1, 2]


5,刪除元素

list.remove(value)     從左至右,刪除第一個元素

list.pop()                     不傳遞index參數,刪除結尾的元素,傳遞index參數,刪除索引為index的元素,並且會返回被刪除的元素

list.clear()                  清空列表

>>> lst
[1, 2, 3, 4, 5, 0, 1, 2]
>>> lst.remove(2)
>>> lst
[1, 3, 4, 5, 0, 1, 2]
>>> lst.pop()
2
>>> lst
[1, 3, 4, 5, 0, 1]
>>> lst.pop(0)
1
>>> lst
[3, 4, 5, 0, 1]

lst.clear()   在python 2.x中沒有,3.x版本才有


6,其他動作

len(list)                     列印列表長度

list.reverse()              反轉列表, == lst[::-1]  切片的操作也可以實現反轉

list.sort()                  使用的是快速排序法


lst 2 = lst  複製,lst2 和lst指向的是同一塊記憶體

>>> lst1 = lst
>>> lst1
[3, 4, 5, 0, 1]
>>> id(lst)
58316296L
>>> id(lst1)
58316296L  


以下內容python2 不支援

lst.copy()  淺複製(影子拷貝),複製操作傳遞的是引用  ,複製操作,對可變對象是引用傳遞,對不可變對象是值傳遞

深拷貝,需要import copy 模組

import copy

lst2 = copy.deepcopy(lst)

此時lst2 和lst的記憶體就不是一塊記憶體了






 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.