好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Python元素集合的列表切片

一、列表切片(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_Lis t时,实际上没有两个列表。 赋值仅将引用复制到列表,而不是实际列表。 因此,赋值后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

到此这篇关于Python元素集合的列表切片的文章就介绍到这了,更多相关Python列表切片内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

查看更多关于Python元素集合的列表切片的详细内容...

  阅读:35次