此外,还可以添加到my other post上。如果我有一个坐标列表,我如何将它们赋值给变量,并保持追加和赋值:
positions = [(1,1), (2,4), (6,7)]
index = 0
for looper in range(0, len(positions)):
posindex = positions[index]
index = index + 1其中posindex是pos0,然后是pos1,然后是pos2,并且随着变量index的增加而增加,这也会给出列表中的索引。Python为我提供了以下内容:
"'posindex' is undefined"怎么把一个变量放到另一个变量里?我可能会遇到的其他问题吗?
发布于 2012-03-04 05:04:26
这段代码运行得很好。然而,还有一种更好的方法:
positions = [(1,1), (2,4), (6,7)]
for posindex in positions:
# do something with posindex, for example:
print (posindex)哪种输出
(1, 1)
(2, 4)
(6, 7)您不需要循环索引- Python可以简单地迭代列表。如果您出于其他原因确实需要索引,以下是如何在Python中执行此操作:
for index, posindex in enumerate(positions):
print ("{0} is at position {1}".format(posindex, index))https://stackoverflow.com/questions/9549837
复制相似问题