I want something like the below code, except the original list's indexes need to be maintained. So for example if i is 5 and the array had 10 elements j will be equal to 5, 6, 7, 8, 9. The below code is not doing that.
我需要类似下面的代码,除了需要维护原始列表的索引。因此,例如,如果i为5且数组具有10个元素,则j将等于5,6,7,8,9。以下代码不会这样做。
for j, compare in enumerate(array[i:]):
#do stuff
Now I can do this in a more C/Java way of doing it, but is there a more pythonic way?
现在我可以用更多的C / Java方式做到这一点,但有更多的pythonic方式吗?
SOLUTION: Thanks to cricket-007 and zamuz for the help.
解决方案:感谢板球-007和zamuz的帮助。
What I originally had is fine, but it can also be done via enumerate. @cricket-007 suggests if only the index is needed go for the original and if the value is needed also, go for enumeration.
我最初拥有的很好,但它也可以通过枚举来完成。 @ cricket-007建议如果只需要索引去原始版本,如果还需要该值,则进行枚举。
Original:
原版的:
for j in range(i, len(array)):
# do stuff
Enumerate:
枚举:
for j, compare in enumerate(array[i:], i):
#do stuff
2 个解决方案
#1
5
Like this?
喜欢这个?
i = 5
for j in range(i, len(array)):
# do stuff
#2
2
How about:
怎么样:
for i, compare in enumerate(array[i:], i):
#do stuff
#1
5
Like this?
喜欢这个?
i = 5
for j in range(i, len(array)):
# do stuff
#2
2
How about:
怎么样:
for i, compare in enumerate(array[i:], i):
#do stuff