網站首頁 編程語言 正文
什么是索引?
- 哪些數據類型里有索引的概念? —> 字符串、列表、元組
- 從最左邊記錄的位置開始就是索引
- 索引用數字表示,起始位是從 0 開始
- 字符串、列表、元組的最大索引是他們的長度 - 1
示例如下:
names = ['Neo', 'Jack', 'Adem'] print(names[0])?? ??? ??? ?# >>> Neo print(names[-1])?? ??? ?# >>> Adem print(names[5])?? ??? ??? ?# IndexError: list index out of range?? ?沒有索引為5的元素,所以報錯
什么是切片?
- 索引用來對單個成員(元素)進行訪問,切片則是對一定范圍內的成員(元素)進行訪問
- 切片通過冒號的方式在中括號內把相隔的兩個索引位置范圍內的成員(元素)找出來,如 [0:10]
- 切片的規則:左含,右不含; 左邊包含,右邊不包含
- 通過切片方式獲取的完整的列表已經不再是原來的列表了,即使獲取的是原來列表的完整的內容
示例如下:
num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] print(num_list[3:7])
執行結果如下:
>>> [4, 5, 6, 7]
獲取列表的完整內容如下:
names = ['Neo', 'Jack', 'Adem'] print('names 列表完整的內容是:', names[:]) print('names 列表完整的內容是:', names[0:])
執行結果如下:
>>> names 列表完整的內容是: ['Neo', 'Jack', 'Adem']
?>>> names 列表完整的內容是: ['Neo', 'Jack', 'Adem']
通過切片方式獲取的完整的列表已經不再是原來的列表了,即使獲取的是原來列表的完整的內容,
示例如下:
names = ['Neo', 'Jack', 'Adem', 'Lily'] print('\'names\' 列表的內存地址為:', id(names)) print('索引獲取的完整\'names\' 列表的內存地址為:', id(names[0:])) # 執行結果如下: # >>> 'names' 列表的內存地址為: 140522949689152 # >>> 索引獲取的完整'names' 列表的內存地址為: 140522949686656?
反序獲取列表成員(元素),示例如下:
num_list = [1, 2, 3, 4, 5, 6, 7, 8] print(num_list[::-1]) ? ? ? # 列表的反序 print(num_list[-3:-1]) ? ? ?# 列表的反向獲取 print(num_list[0:8:2]) ? ? ?# 列表的步長獲?。骸久扛?步長(也可以理解為每兩個數值)獲取索引0到8的元素】 # 執行結果如下: # >>> [8, 7, 6, 5, 4, 3, 2, 1] # >>> [6, 7] # >>> [1, 3, 5, 7]
切片生成空列表,示例如下:
num_list = [1, 2, 3, 4, 5, 6, 7, 8] print(num_list[0:0])? # 執行結果如下: # >>> []
列表的索引,獲取與修改
- list.index(item) 列表通過 index() 函數,傳入一個元素來獲取當前元素的索引值
- list[index] = new_item ; list[index]為變量對應的索引的值; new_item 為一個新的元素
- 數據修改的范圍只能是已存在的索引范圍內
- 列表無法通過添加新的索引的方式賦值
示例如下:
test_str = ['a', 'b', 'c', 'd'] print(test_str.index('c')) # 執行結果如下: # >>> 2?? ??? ??? ?'c' 的索引位置是 2 test_str = ['a', 'b', 'c', 'd'] print(test_str.index('e')) # 執行結果如下: # >>> ValueError: 'e' is not in list
test_str = ['a', 'b', 'c', 'd'] test_str[0]='z' print(test_str) # 執行結果如下: # >>> ['z', 'b', 'c', 'd'] test_str = ['a', 'b', 'c', 'd'] test_str[:]='h', 'j', 'k', 'l' print(test_str) # 執行結果如下: # >>> ['h', 'j', 'k', 'l'] test_str = ['a', 'b', 'c', 'd'] test_str[:]=['o', 'p', 'q', 'r'] print(test_str) # 執行結果如下: # >>> ['o', 'p', 'q', 'r']
test_str = ['a', 'b', 'c', 'd'] test_str[5]='z' print(test_str) # 執行結果如下: # >>> IndexError: list assignment index out of range
通過 pop() 函數刪除索引
pop() 函數的功能:通過索引刪除并獲取列表的元素
pop() 函數的用法: list.pop(index) , index 為刪除列表的第幾個元素
- 函數會刪除該索引的元素并返回
- 如果傳入的 index 索引不存在,則會報錯
示例如下:
names = ['Neo', 'Jack', 'Adem', 'Lily'] pop_item = names.pop(1) print('刪除的元素為:', pop_item, ', 被刪除后的\'names\'列表為:', names) # 執行結果如下: # >>> 刪除的元素為: Jack , 被刪除后的'names'列表為: ['Neo', 'Adem', 'Lily'] names = ['Neo', 'Jack', 'Adem', 'Lily'] pop_item = names.pop(5) print(names) # 執行結果如下: # >>> IndexError: pop index out of range
通過 del 刪除索引
del 函數的功能:通過索引刪除并獲取列表的元素
del 函數的用法: del list(index) , index 為刪除列表的第幾個元素
- 直接刪除,無返回值
- 如果傳入的 index 索引不存在,則會報錯
示例如下:
names = ['Neo', 'Jack', 'Adem', 'Lily'] del names[0] print(names) # 執行結果如下: # >>> ['Jack', 'Adem', 'Lily'] names = ['Neo', 'Jack', 'Adem', 'Lily'] del names[0] print(names) # 執行結果如下: # >>> IndexError: list assignment index out of range
索引在元組中的特殊性
- 可以和列表 一樣獲取索引與切片索引
- 元組函數 index 和列表的用法完全一致
- 無法通過索引修改、刪除元素(因為元組是不可修改的)
原文鏈接:https://blog.csdn.net/weixin_42250835/article/details/123153519
相關推薦
- 2022-04-15 詳解Python?prometheus_client使用方式_python
- 2022-06-09 FreeRTOS實時操作系統的任務創建與任務切換_操作系統
- 2022-11-06 React?hook實現簡單的websocket封裝方式_React
- 2022-08-07 Python算法練習之二分查找算法的實現_python
- 2022-09-25 文本文件與二進制文件的區別
- 2022-12-13 Python?urllib?入門使用詳細教程_python
- 2022-03-27 關于Android?Device?Monitor?無法打開問題_Android
- 2022-04-12 解決error: failed to push some refs to ‘xxx(遠程倉庫)‘
- 最近更新
-
- 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同步修改后的遠程分支