新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python切片符號的使用

10年積累的網(wǎng)站制作、網(wǎng)站設計經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡服務。我雖然不認識你,你也不認識我。但先網(wǎng)站制作后付款的網(wǎng)站建設流程,更有蕭縣免費網(wǎng)站建設讓你可以放心的選擇與我們合作。
a[start:stop] # items start through stop-1 a[start:] # items start through the rest of the array a[:stop] # items from the beginning through stop-1 a[:] # a copy of the whole array
還有一個step值,可以與上述任何一個一起使用:
a[start:stop:step] # start through not past stop, by step
要記住的關鍵點是該:
1、stop值表示不在所選切片中的第一個值。之間的差stop和start是選擇的元素的數(shù)量(如果step是1,默認值)。
2、startorstop可能是一個負數(shù),這意味著它從數(shù)組的末尾而不是開頭開始計數(shù)。
所以:
a[-1] # last item in the array a[-2:] # last two items in the array a[:-2] # everything except the last two items
同樣,step可能是負數(shù):
a[::-1] # all items in the array, reversed a[1::-1] # the first two items, reversed a[:-3:-1] # the last two items, reversed a[-3::-1] # everything except the last two items, reversed
如果項目少于您的要求,Python 對程序員是友好的。例如,如果你請求a[:-2]并且a只包含一個元素,你會得到一個空列表而不是錯誤。有時您更喜歡錯誤,因此您必須意識到這可能會發(fā)生。
以上就是python切片符號的使用,希望對大家有所幫助。更多Python學習指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
新聞名稱:創(chuàng)新互聯(lián)Python教程:python切片符號的使用
文章路徑:http://www.5511xx.com/article/dheghoc.html


咨詢
建站咨詢
