網站首頁 編程語言 正文
一、列表切片(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
原文鏈接:https://blog.csdn.net/bai666ai/article/details/123024601
相關推薦
- 2022-09-18 Pandas?Query方法使用深度總結_python
- 2022-06-01 Python處理日期和時間的方法總結_python
- 2022-12-03 C++實現重載矩陣的部分運算符_C 語言
- 2022-06-27 精簡高效的C#網站優化經驗技巧總結_C#教程
- 2022-09-18 ASP.NET?Core實現文件上傳和下載_實用技巧
- 2022-06-08 FreeRTOS動態內存分配管理heap_1示例_操作系統
- 2022-04-11 matlab模擬退火算法單約束車間流水線調度解決實現及示例_C 語言
- 2022-07-06 C++數據結構深入探究棧與隊列_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支