Python操作列表、字典、元組

說明:
Python內(nèi)置的數(shù)據(jù)類型有:列表(list)、字典(dictionary)、元組(tuple)、字符串(string)、集合(set)
本次重點:
Python操作列表、字典、元組

一、列表(list)

1.特性:list是一種有序的集合,可以隨時添加和刪除其中的元素。
2.創(chuàng)建:一對方括號“[]”和其包含的元素,單個元素可以不加逗號,同元組一樣,可以創(chuàng)建嵌套列表。如:

tempList = ["one","two","three"]
#列表元素可以是不同數(shù)據(jù)類型的
L = ['Apple', 123, True]

3.基本操作及方法:
(1)訪問:
3.1.1 訪問一般列表
用索引來訪問list中每一個位置的元素,記得索引是從0開始的:

>>> tempList [0]
'one'
>>> tempList [1]
'two'
>>> tempList [2]
'three'
>>> tempList [3]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

當(dāng)索引超出了范圍時,Python會報一個IndexError錯誤,所以,要確保索引不要越界,記得最后一個元素的索引是len(tempList ) - 1。

如果要取最后一個元素,除了計算索引位置外,還可以用-1做索引,直接獲取最后一個元素:

>>> tempList [-1]
'three'

以此類推,可以獲取倒數(shù)第2個、倒數(shù)第3個:

>>> tempList [-2]
'two'
>>> tempList [-3]
'one'
>>> tempList [-4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

當(dāng)然,倒數(shù)第4個就越界了。

3.1.2 訪問嵌套列表
如,列表如下:

>>> s = ['python', 'java', ['asp', 'php'], 'scheme']
>>> len(s)
4

訪問:

s[2][1] -> 'php'

(2)遍歷
可用for循環(huán)遍歷列表

tempList = ["one","two","three"]
#用for循環(huán)遍歷列表
for element in tempList:
    print(element)
>>
one
two
three

(3)求長
len(列表名)可實現(xiàn)求長。

tempList = ["one","two","three"]
#長度
print(len(tempList))
>>
3

(4)打印
print(列表名)

tempList = ["one","two","three"]
#打印列表
print(tempList)
>>
['one', 'two', 'three']

(5)類型
type(列表名)

tempList = ["one","two","three"]
#查看類型
print(type(tempList))
>>
<class 'list'>

(6)追加
往list中追加元素到末尾:

tempList = ["one","two","three"]
#追加
tempList.append("four")
print(tempList)
>>
['one', 'two', 'three', 'four']

把元素插入到指定的位置,比如索引號為1的位置:

tempList = ["one","two","three"]
#把元素插入到指定的位置
tempList.insert(2,"four")
print(tempList)
>>
['one', 'two', 'four', 'three']

(7)替換
把某個元素替換成別的元素,可以直接賦值給對應(yīng)的索引位置:

tempList = ["one","two","three"]
#替換元素
tempList[1] = "test"
print(tempList)
>>
['one', 'test', 'three']

(8)刪除
刪除list末尾的元素,用pop()方法:

tempList = ["one","two","three"]
#刪除最后一個元素
tempList.pop()
"three"
print(tempList)
>>
['one', 'two']

要刪除指定位置的元素,用pop(i)方法,其中i是索引位置:

tempList = ["one","two","three"]
#刪除位置2的元素
tempList.pop(1)
"two"
print(tempList)
>>
['one', 'three']

4.高級操作及方法:

(1)產(chǎn)生一個數(shù)值遞增列表:

  • pList = range(N),產(chǎn)生一個元素值為0~N-1的列表。如:pList = range(10) ->pList = [0,1,2,3,...,9]。

  • pList = range(sn,en),產(chǎn)生一個元素值為sn~en-1的列表。如:pList =
    range(1,5) ->pList = [1,2,3,4]。

  • pList =range(sn,en,inc),產(chǎn)生一個元素值以inc遞增的列表。如:pList = range(1,10,2) ->pList =
    [1,3,5,7,9]

(2)固定值初始化:pList = [value for index in range(N)],產(chǎn)生一個長度為N的列表,元素值都為value.

如:value = "x",N=10,則pLsit = ["x","x",...,"x"]

更簡單的形式:pList = [value]*N。

(3)操作符:

  • "+":兩個列表相加,將合成為一個列表。如 pL1 = [1,2],pL2 = [3,4],pL1+pL2 ->[1,2,3,4]

  • "":形式:[value]N。如value="a",N=4,則得到列表["a","a","a","a"]。

  • del:del pL[index]:刪除指定位置的元素。 del pL[sIndex:eIndex]:刪除sIndex~eIndex-1位置的元素。

(4)列表復(fù)制:

  • pL1 = pL:pL1為pL的別名,即pL1和pL實質(zhì)為同一個列表,修改其中一個列表,另一個列表也隨之變化。
    如:pL = [1,2,3],pL1 = pL,pL1[2] = 33,->pL = pL1 = [1,2,33]

  • pL2 = pL[:]:pL2為pL的一個克?。ɑ蚩截悾磒L2和pL為不同的列表,修改其中一個列表,另一個不會受影響。

(5)常用方法:

  • L.append(value):向列表末尾插入一個元素

  • L.insert(index,value):向列表的index位置插入一個元素value。

  • L.pop(index): 從列表中刪除指定位置index的元素并返回元素值,默認(rèn)刪除最后一個元素。

  • L.remove(value):刪除在列表中第一次出現(xiàn)的元素value。如:pList = [1,2,3,2],pList.remove(2) ->pList = [1,3,2]。

  • L.count(value):返回元素value在列表中出現(xiàn)的次數(shù)。

  • L.index(value) :該元素第一次出現(xiàn)的的位置,無則拋異常 。

  • L.extend(list/tuple/string) :向列表末尾插入多個元素。

  • L.sort():排序

  • L.reverse():倒序

二、 字典(dictionary)

三、元組(tuple)

1.特性:元祖是一種有序列表。tuple和list非常類似,但是tuple一旦初始化就不能修改?!疽坏﹦?chuàng)建元組,則這個元組就不能被修改,即不能對元組進(jìn)行更新、增加、刪除操作】
tuple的優(yōu)勢: 因為tuple不可變,所以代碼更安全。如果可能,能用tuple代替list就盡量用tuple。
2.創(chuàng)建:一對圓括號“()”和其包含的元素(若沒有元素,則為空元組),如:

tempTuple = ("one","two","three")
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容