Python元組索引、截取


Python元組索引、截取:

索引下標:

tuple_1 = ('a','b','c','d','e','f','g','h') print(tuple_1[0]) # a print(tuple_1[3]) # d print(tuple_1[7]) # h # 當索引下標為負數時,-1表示最右端元素,從右向左依次遞減 print(tuple_1[-1]) # h print(tuple_1[-4]) # e

切片操作:

# 使用切片進行截取列表元素  tuple_1 = (1,2,3,4,5,6,7,8,9,10) print(tuple_1[::]) # (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) print(tuple_1[2:8]) # (3, 4, 5, 6, 7, 8) print(tuple_1[2:8:3]) # (3, 6) , 不包含end print(tuple_1[2::-1]) # (3, 2, 1) print(tuple_1[8:1:-1]) # (9, 8, 7, 6, 5, 4, 3) print(tuple_1[8:1:-2]) # (9, 7, 5, 3) print(tuple_1[-1:-5:-1]) # (10, 9, 8, 7)

2020-02-09


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM