python步长是什么?
步长是切片里的step,step不能为0,默认为1。(Python中提供两种索引:从左向右 0 ….. index-1 从右向左 -1 …. -index),关键是在这个step的含义。
步长判断
若 step > 0, 则示意从左向右举行切片。此时,start必需小于end才有效果,否则为空。比方: s[0,: 5: 2]的效果是’ace’。
若 step < 0, 则示意从右向左举行切片。 此时,start必需大于end才有效果,否则为空。列如: s[5: 0: -1]的效果是’fedcb’。
实例
列表重复步长删除元素。def last_item(lt, step):
while len(lt) >= step and step != 1:
lt.pop(step - 1)
# print(lt)
lt = lt[step - 1:] + lt[:step - 1]
while len(lt)
n = step % len(lt)
lt.pop(n - 1)
else:
if step == 1:
return (lt[-1])
else:
return lt[0]```