日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python基础教程:列表(list)切片详细操作

發布時間:2025/3/20 python 10 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python基础教程:列表(list)切片详细操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.正向范圍取值

關鍵點

  • 首位下標是 0
  • 第一個數字是起始下標,第二個數字是結束下標(但最終結果不包含它)

例一:

# 正向范圍取值 - 字符串 strs ="https://www.baidu.com/"# 從第 0 個下標開始取值,到第 1 個下標結束,但不會取第 1 個下標的元素,最終取的是 0 下標的值 print(strs[0:1])# 從第 0 個下標開始取值,到第 10 個下標結束,但不會取第 10 個下標的元素,最終取的是 1,2,3,4,5,6,7,8,9 下標的值 print(strs[0:10])# 從第 5 個下標開始取值,到第 10 個下標結束,但不會取第 10 個下標的元素,最終取的是 5,6,7,8,9 下標的值 print(strs[5:10])# 從第 5 個下標開始取值,到第 100 個下標結束,但因為字符串最長就 30 個字符,所以會取到最后一個結束就結束了 print(strs[5:100])# 相同數字返回空 print(strs[5:5])# 第二個數字比第一個數字小,返回空 print(strs[5:4])# 從第 0 個下班開始取值,取后面所有元素 print(strs[0:])# 取前面 10 個元素 print(strs[:10])

運行結果

h https://ww ://ww ://www.baidu.com/https://www.baidu.com/ https://ww

例二:

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' # 正向范圍取值 - 數組 lists = [1, 2, 3, 4, 5, 6, 7]print(lists[0:1]) print(lists[0:10]) print(lists[5:10]) print(lists[5:100]) print(lists[5:5]) print(lists[5:4])

運行結果

[1] [1, 2, 3, 4, 5, 6, 7] [6, 7] [6, 7] [] []

2.反向范圍取值

關鍵點

  • 因為是反向,所以倒數的下標從 -1 算起
  • 第一個數字是起始下標,第二個數字是結束下標(但最終結果不包含它)
  • 第一個數字是負數情況下,第二個數字最大是 -1,如果寫成 0 會返回空值

例:

# 反向范圍取值 - 字符串 strs = "https://www.baidu.com/"# 取最后 10 個元素 print(strs[-10:])# 取最后 6-10 的元素, 不會取到倒數第五個元素 print(strs[-10:-5])# 反向范圍取值 - 列表 lists = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# 取最后 6 個元素 print(lists[-6:])# 取最后 5 個元素, 但不會取到倒數第 1 個元素 print(lists[-5:-1])# 第二個值寫0,返回空值 print(lists[-10:0])# 正數+復數組合 print(lists[1:-5])

運行結果

baidu.com/ baidu [5, 6, 7, 8, 9, 10] [6, 7, 8, 9] [] [2, 3, 4, 5]

3.[:] 復制對象

例:

''' 學習中遇到問題沒人解答?小編創建了一個Python學習交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' lists1 = [1, 2, 3, 4, 5] lists2 = lists1 lists1.append(6) print(lists1, lists2, id(lists1), id(lists2))lists1 = [1, 2, 3, 4, 5] lists2 = lists1[:] lists1.append(6) print(lists1, lists2, id(lists1), id(lists2))lists1 = [1, 2, 3, 4, 5, [1, 2, 3]] lists2 = lists1 lists1[5].append(4) print(lists1, lists2, id(lists1), id(lists2))lists1 = [1, 2, 3, 4, 5, [1, 2, 3]] lists2 = lists1[:] lists1[5].append(4) print(lists1, lists2, id(lists1), id(lists2))strs1 = "abcd" strs2 = strs1 strs1 = "abc" print(strs1, strs2, id(strs1), id(strs2))strs1 = "abcd" strs2 = strs1[:] strs1 = "abc" print(strs1, strs2, id(strs1), id(strs2))

運行結果

[1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6] 2560550555144 2560550555144 [1, 2, 3, 4, 5, 6] [1, 2, 3, 4, 5] 2560550627784 2560548023880 [1, 2, 3, 4, 5, [1, 2, 3, 4]] [1, 2, 3, 4, 5, [1, 2, 3, 4]] 2560550627400 2560550627400 [1, 2, 3, 4, 5, [1, 2, 3, 4]] [1, 2, 3, 4, 5, [1, 2, 3, 4]] 2560550627784 2560550627656 abc abcd 2560547930776 2560548937376 abc abcd 2560547930776 2560548937376

[:] 等同于淺拷貝,對可變對象是生效的

4.[::] 步進

例:

# [::] strs = "https://www.baidu.com/"# 取最后 10 個元素,每 2 個取 1 個 print(strs[-10::2])# 取第 0 到 10 的元素,每 5個 取 1 個 print(strs[0:10:5])print(strs[::]) # 倒序 print(strs[::-1])lists = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# 取全部元素,每 3 個 取 1 個 print(lists[::3]) # 倒序 print(lists[::-1])

運行結果

biucm h: https://www.baidu.com/ /moc.udiab.www//:sptth [1, 4, 7, 10] [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

結尾給大家推薦一個非常好的學習教程,希望對你學習Python有幫助!

Python基礎入門教程推薦:更多Python視頻教程-關注B站:Python學習者
https://www.bilibili.com/video/BV1LL4y1h7ny?share_source=copy_web

Python爬蟲案例教程推薦:更多Python視頻教程-關注B站:Python學習者
https://www.bilibili.com/video/BV1QZ4y1N7YA?share_source=copy_web

總結

以上是生活随笔為你收集整理的Python基础教程:列表(list)切片详细操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。