新聞中心

10年積累的網(wǎng)站制作、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有蕭縣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
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
還有一個(gè)step值,可以與上述任何一個(gè)一起使用:
a[start:stop:step] # start through not past stop, by step
要記住的關(guān)鍵點(diǎn)是該:
1、stop值表示不在所選切片中的第一個(gè)值。之間的差stop和start是選擇的元素的數(shù)量(如果step是1,默認(rèn)值)。
2、startorstop可能是一個(gè)負(fù)數(shù),這意味著它從數(shù)組的末尾而不是開頭開始計(jì)數(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可能是負(fù)數(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
如果項(xiàng)目少于您的要求,Python 對(duì)程序員是友好的。例如,如果你請(qǐng)求a[:-2]并且a只包含一個(gè)元素,你會(huì)得到一個(gè)空列表而不是錯(cuò)誤。有時(shí)您更喜歡錯(cuò)誤,因此您必須意識(shí)到這可能會(huì)發(fā)生。
以上就是python切片符號(hào)的使用,希望對(duì)大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:python切片符號(hào)的使用
標(biāo)題鏈接:http://m.5511xx.com/article/dheghoc.html


咨詢
建站咨詢
