输出索引和元素使用范围

时间:2017-06-10 16:39:25

标签: python python-2.7

如何解决这个问题 预期产出:

1 - that
2 - is
3 - weird

代码:

list = ['that', 'is', 'weird']
for i, k in range(list):
     print str(i) + ' - ' + k

1 个答案:

答案 0 :(得分:0)

尝试枚举而不是范围:

words = ['that', 'is', 'weird']
for i, k in enumerate(words):
     print str(i) + ' - ' + k