溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python元素集合的列表切片實例分析

發(fā)布時間:2022-02-21 09:30:08 來源:億速云 閱讀:213 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下Python元素集合的列表切片實例分析的相關(guān)知識點,內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

一、列表切片(Slicing)

由于列表是元素的集合,我們應(yīng)該能夠獲得這些元素的任何子集。 例如,如果想從列表中獲得前三個元素,我們應(yīng)該能夠輕松地完成。 對于列表中間的任何三個元素,或最后三個元素,或列表中任何位置的任何x個元素,情況也應(yīng)如此。 列表的這些子集稱為切片。

If L is a list, the expression L [ start : stop : step ] returns the portion of the list from index start to index stop, at a step size step.

Python元素集合的列表切片實例分析

二、基礎(chǔ)實例

下面是列表切片的一個基本示例:

Python元素集合的列表切片實例分析

#Example: Slice from index 2 to 7

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[2:7])    # ['c', 'd', 'e', 'f', 'g']

['c', 'd', 'e', 'f', 'g']

三、帶有負(fù)索引的切片 (Slice with Negative Indices)

可以在切片列表時指定負(fù)索引。

例如: Slice from index -7 to -2、

Python元素集合的列表切片實例分析

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[-7:-2])    # ['c', 'd', 'e', 'f', 'g']
['c', 'd', 'e', 'f', 'g']

四、帶有正負(fù)索引的切片

可以同時指定正索引和負(fù)索引。

# Slice from index 2 to -5

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[2:-5])    # ['c', 'd']
['c', 'd']

五、指定切片step

可以使用step參數(shù)指定切片的步長。

step參數(shù)是可選的,默認(rèn)情況下為1。

Python元素集合的列表切片實例分析

#Returns every 2nd item between position 2 to 7

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[2:7:2])    # ['c', 'e', 'g']
['c', 'e', 'g']

六、負(fù)步長

可以指定負(fù)步長。

#Example: Returns every 2nd item between position 6 to 1

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[6:1:-2])    # ['g', 'e', 'c']
['g', 'e', 'c']

七、在開始和結(jié)束處切片 (Slice at Beginning & End)

省略起始索引會從索引0開始切片。

含義,L [:stop]等效于L [0:stop]

# Example: Slice the first three items from the list

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[:3])    # ['a', 'b', 'c']
['a', 'b', 'c']

而省略stop索引會將切片延伸到列表的末尾。

意思是L [start:]等效于L [start:len(L)]

Example: 從列表中切掉最后三項

L = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
print(L[6:])    # ['g', 'h', 'i']
['g', 'h', 'i']

八、反轉(zhuǎn)列表 (Reverse a List)

可以通過省略開始索引和停止索引并將步驟指定為-1來反轉(zhuǎn)列表。

Example: 使用切片運(yùn)算符反轉(zhuǎn)列表

L = ['a', 'b', 'c', 'd', 'e']
print(L[::-1])
['e', 'd', 'c', 'b', 'a']

九、修改多個列表元素值

可以使用切片賦值一次修改多個列表元素。

Example: 使用slice修改多個列表項

L = ['a', 'b', 'c', 'd', 'e']
L[1:4] = [1, 2, 3]
print(L)    # ['a', 1, 2, 3, 'e']
['a', 1, 2, 3, 'e']

Example: 替換多個元件以代替單個元件

L = ['a', 'b', 'c', 'd', 'e']
L[1:2] = [1, 2, 3]
print(L)    # ['a', 1, 2, 3, 'c', 'd', 'e']
['a', 1, 2, 3, 'c', 'd', 'e']

十、插入多個列表元素

我們可以在列表中插入項目,而無需替換任何內(nèi)容。只需指定

Example: 使用slice插入多個列表項

L = ['a', 'b', 'c']
L[:0] = [1, 2, 3]
print(L)    # [1, 2, 3, 'a', 'b', 'c']
[1, 2, 3, 'a', 'b', 'c']
L = ['a', 'b', 'c']
L[len(L):] = [1, 2, 3]
print(L)    # ['a', 'b', 'c', 1, 2, 3]
['a', 'b', 'c', 1, 2, 3]

可以通過指定切片的開始索引和停止索引將元素插入到列表的中間。

Example:在中間插入多個列表項

L = ['a', 'b', 'c']
L[1:1] = [1, 2, 3]
print(L)    # ['a', 1, 2, 3, 'b', 'c']
['a', 1, 2, 3, 'b', 'c']

十一、刪除多個列表元素

可以通過將適當(dāng)?shù)那衅x值給空列表來刪除列表中間的多個元素。

也可以將del語句用于切片。

Example: 使用slice刪除多個列表項

L = ['a', 'b', 'c', 'd', 'e']
L[1:5] = []
print(L)    # ['a']
['a']
with del keyword
L = ['a', 'b', 'c', 'd', 'e']
del L[1:5]
print(L)    # ['a']
['a']

十二、克隆或復(fù)制列表

當(dāng)執(zhí)行new_List = old_List時,實際上沒有兩個列表。 賦值僅將引用復(fù)制到列表,而不是實際列表。 因此,賦值后new_List和old_List都引用相同的列表。

可以使用切片運(yùn)算符復(fù)制列表(也稱為淺拷貝)。

Example: 使用slice創(chuàng)建列表的副本(淺拷貝)

L1 = ['a', 'b', 'c', 'd', 'e']
L2 = L1[:]
print(L2)       # ['a', 'b', 'c', 'd', 'e']
print(L2 is L1) # False
['a', 'b', 'c', 'd', 'e']
False

以上就是“Python元素集合的列表切片實例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI