日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Python元素集合的列表切片_python

作者:bai666ai ? 更新時間: 2022-04-24 編程語言

一、列表切片(Slicing)

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

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.

二、基礎實例

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

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

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

可以在切片列表時指定負索引。

例如: 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']

四、帶有正負索引的切片

可以同時指定正索引和負索引。

# 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參數指定切片的步長。

step參數是可選的,默認情況下為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']

六、負步長

可以指定負步長。

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

七、在開始和結束處切片 (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']

八、反轉列表 (Reverse a List)

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

Example: 使用切片運算符反轉列表

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

十、插入多個列表元素

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

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

十一、刪除多個列表元素

可以通過將適當的切片賦值給空列表來刪除列表中間的多個元素。

也可以將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']

十二、克隆或復制列表

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

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

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

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

原文鏈接:https://blog.csdn.net/bai666ai/article/details/123024601

欄目分類
最近更新