【发布时间】:2021-02-22 14:17:49
【问题描述】:
现在我有如下所示的元组列表:
[(78, -32), (54, -32), (30, -32), (30, -8), (30, 16), (30, 40), (30, 64), (6, 64), (-18, 64), (-42, 64), (-66, 64), (-66, 88), (-90, 88), (-114, 88)]
我目前的代码是这样的:
i = 13 # Let i start at index 13
tech = [] # Define list
while (x, y) != (start_x, start_y): # while loop to iterate through all the coordinates until the path has been found
tech.append(solution[x,y]) # Appends the coordinates to tech list
x, y = solution[x, y] # get x and y coordinates
for i in tech: # Loop thorugh each tuple
print(i) # Print each tuple
# time.sleep(1)
i -= 1 # Decrement the index
我想要做的是以相反的顺序打印出列表,从前面的最后一个元组坐标和后面的第一个元组坐标开始。现在的问题是,当我尝试减少索引时,它会引发此错误:
unsupported operand type(s) for -=: 'tuple' and 'int'
有人知道为什么吗?
【问题讨论】:
-
所以
[(-114, 88), (-90, 88)], ...? -
i不是索引,而是元组本身。 -
@Ironkey 是的,这就是我要找的
-
打印(x[::-1])
标签: python python-3.x