Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 输出索引和元素使用范围_Python_Python 2.7 - Fatal编程技术网

Python 输出索引和元素使用范围

Python 输出索引和元素使用范围,python,python-2.7,Python,Python 2.7,如何解决这个问题 预期产出: 1 - that 2 - is 3 - weird 守则: list = ['that', 'is', 'weird'] for i, k in range(list): print str(i) + ' - ' + k 尝试枚举而不是范围: words = ['that', 'is', 'weird'] for i, k in enumerate(words): print str(i) + ' - ' + k 为什么你认为range(l

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

1 - that
2 - is
3 - weird
守则:

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

尝试枚举而不是范围:

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

为什么你认为
range(list)
会给你一组
i,k
?您是否正在考虑
enumerate
?提供/阅读此代码生成的回溯将非常有用,或者,如果您想更具python风格:
print'\n'。加入([(str(i)+'-'+k)for i,k in enumerate(words)]