避免Python循环中的计数器增量

时间:2013-08-23 03:06:47

标签: python

如何避免在以下Python代码中手动递增sequence计数器:

sequence = 0
for value in random.sample(range(1000), 7):
  # do something with value and sequence
  sequence += 1

1 个答案:

答案 0 :(得分:8)

枚举!您可以参考Python docs

for sequence, value in enumerate(random.sample(range(1000), 7)):
    # do something with value and sequence