Python列表截取:
使用索引下标查看列表元素:
lst = ['a','b','c','d','e','f','g','h']
print(lst[0])
# a
print(lst[3])
# d
print(lst[7])
# h
# 当索引下标为负数时,-1表示最右端元素,从右向左依次递减
print(lst[-1])
# h
print(lst[-4])
# e
列表名[start:end:step]:
使用切片截取列表元素(不包含end)
# 使用切片进行截取列表元素
# lst = [i for i in range(1,11)]
lst = [1,2,3,4,5,6,7,8,9,10]
print(lst[::])
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(lst[2:8])
# [3, 4, 5, 6, 7, 8]
print(lst[2:8:3])
# [3, 6] , 不包含end
print(lst[2::-1])
# [3, 2, 1]
print(lst[8:1:-1])
# [9, 8, 7, 6, 5, 4, 3]
print(lst[8:1:-2])
# [9, 7, 5, 3]
print(lst[-1:-5:-1])
# [10, 9, 8, 7]
+= 对列表进行拼接操作:
# += 对列表进行拼接操作:
lst = [1,2,3]
lst += [4,5,6]
print(lst)
# [1, 2, 3, 4, 5, 6]
2020-02-09