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