17
数组我遍历数组在Python:如何输出指数在遍历在Python
for g in [ games[0:4] ]:
g.output()
可我还初始化并在for
循环增量索引,并将其传递给g.output()
?
这样g.output(2)
结果:
Game 2 - ... stuff relating to the object `g` here.
数组我遍历数组在Python:如何输出指数在遍历在Python
for g in [ games[0:4] ]:
g.output()
可我还初始化并在for
循环增量索引,并将其传递给g.output()
?
这样g.output(2)
结果:
Game 2 - ... stuff relating to the object `g` here.
像这样:
for index, g in enumerate(games[0:4]):
g.output(index)
使用内置enumerate
方法:
for i,a in enumerate(['cat', 'dog']):
print '%s is %d' % (a, i)
# output:
# cat is 0
# dog is 1
这两个答案都是一样的,但你是更详细。感谢那。 – aimbire 2017-09-18 13:36:14