3

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

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

1 回答 1

8

枚举!您可以参考 Python文档

for sequence, value in enumerate(random.sample(range(1000), 7)):
    # do something with value and sequence
于 2013-08-23T03:07:44.717 回答