欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python元素集合的列表切片

 更新時(shí)間:2022年02月20日 12:11:49   作者:bai666ai  
這篇文章主要介紹了Python元素集合的列表切片,列表是元素的集合,列表的這些子集稱為切片,下面違章圍繞Python列表切片得相關(guān)資料展開學(xué)習(xí)內(nèi)容,需要的朋友可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助

一、列表切片(Slicing)

由于列表是元素的集合,我們應(yīng)該能夠獲得這些元素的任何子集。 例如,如果想從列表中獲得前三個(gè)元素,我們應(yīng)該能夠輕松地完成。 對(duì)于列表中間的任何三個(gè)元素,或最后三個(gè)元素,或列表中任何位置的任何x個(gè)元素,情況也應(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.

二、基礎(chǔ)實(shí)例

下面是列表切片的一個(gè)基本示例:

#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)

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

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

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ù)索引的切片

可以同時(shí)指定正索引和負(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ù)指定切片的步長(zhǎng)。

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

#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ù)步長(zhǎng)

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

#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)

省略起始索引會(huì)從索引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索引會(huì)將切片延伸到列表的末尾。

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

Example: 從列表中切掉最后三項(xiàng)

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來(lái)反轉(zhuǎn)列表。

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

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

九、修改多個(gè)列表元素值

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

Example: 使用slice修改多個(gè)列表項(xiàng)

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: 替換多個(gè)元件以代替單個(gè)元件

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']

十、插入多個(gè)列表元素

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

Example: 使用slice插入多個(gè)列表項(xiàng)

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:在中間插入多個(gè)列表項(xiàng)

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

十一、刪除多個(gè)列表元素

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

也可以將del語(yǔ)句用于切片。

Example: 使用slice刪除多個(gè)列表項(xiàng)

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時(shí),實(shí)際上沒有兩個(gè)列表。 賦值僅將引用復(fù)制到列表,而不是實(shí)際列表。 因此,賦值后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

到此這篇關(guān)于Python元素集合的列表切片的文章就介紹到這了,更多相關(guān)Python列表切片內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論