14. Python的列表切片

《Python编程的术与道:Python语言入门》视频课程
《Python编程的术与道:Python语言入门》视频课程链接:https://edu.csdn.net/course/detail/27845

列表切片(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.

在这里插入图片描述

Basic Example

Here is a basic example of list slicing.
在这里插入图片描述

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

可以在切片列表时指定负索引。

Example: 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: Slice the last three items from the list

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

反转列表 (Reverse a List)

可以通过省略开始索引和停止索引并将步骤指定为-1来反转列表。

#Example: Reverse a list with slicing operator

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

修改多个列表元素值

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

#Example: Modify multiple list items using 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: Replace multiple elements in place of a single element
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']

插入多个列表元素

You can insert items into a list without replacing anything. Simply specify a zero-length slice.

#Example: Insert multiple list items using slice

# Insert at the start
L = ['a', 'b', 'c']
L[:0] = [1, 2, 3]
print(L)    # [1, 2, 3, 'a', 'b', 'c']
[1, 2, 3, 'a', 'b', 'c']
# Insert at the end
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: Insert multiple list items in the middle

# Insert in middle
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: Delete multiple list items using slice

# assign empty list to 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: Create a copy of a list using slice (shallow copy)

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Python列表切片是从原始列表中提取列表的一部分的过程。在列表切片中,我们可以根据需要指定切片的开始位置、结束位置和步长来剪切列表。通过使用切片操作,我们可以轻松地获取列表中的特定元素子集。 举个例子,如果我们有一个列表`ll = [5, 17, 13, 14, 8, 19, 3, 7, 9, 12]`,我们可以使用切片操作来提取其中的一部分元素。例如,如果我们想提取列表中的前三个元素,我们可以使用切片`ll[:3]`,这将返回一个新的列表`[5, 17, 13]`。同样地,我们也可以使用切片操作来获取列表中的后几个元素,比如`ll[5:]`将返回列表中从索引5开始到末尾的所有元素。 除了提取元素,我们还可以通过切片操作来删除和插入多个元素。例如,我们可以使用切片操作`ll[5:] = []`将列表中的后几个元素删除,或者使用切片操作`ll[:0] = ['a', 'b']`将元素`'a'`和`'b'`插入到列表的开头。 总结起来,Python列表切片是一种灵活的操作,可以方便地对列表进行切割、删除和插入操作,使得我们可以更加高效地处理列表数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python 列表切片详解](https://blog.csdn.net/hlx20080808/article/details/127610664)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bai666ai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值