Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.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 3.x - Fatal编程技术网

Python 如何颠倒列表中元组的顺序

Python 如何颠倒列表中元组的顺序,python,python-3.x,Python,Python 3.x,现在我有一个元组列表,如下所示: [(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) !=

现在我有一个元组列表,如下所示:

[(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'
有人知道原因吗?

您可以使用
.reverse()
函数:

data= [(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)]
data.reverse()
print(data)

在这里,
i
是用于在
tech
列表中迭代的变量,因此,如果要向后打印项目,则将每个元组递减1是无效的:

print(*reversed(tech),sep="\n")
在位反转:-)

或者每次迭代使用不同的变量(您的目的是减少
i
?):


您可以使用切片来完成此操作

x = [(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)]

print(x[::-1])
输出

[(-114, 88), (-90, 88), (-66, 88), (-66, 64), (-42, 64), (-18, 64), (6, 64), (30, 64), (30, 40), (30, 16), (30, -8), (30, -32), (54, -32), (78, -32)]

使用
[:-1]
将反转列表值。

所以
[(-114,88),(-90,88)],…
i
不是索引,而是元组本身。@Ironkey是的,这就是我要打印的内容(x[:-1]),请不要只发布代码作为答案,但也要解释代码的作用以及它如何解决问题。带有解释的答案通常更有帮助,质量更好,更容易吸引选票。
x = [(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)]

print(x[::-1])
[(-114, 88), (-90, 88), (-66, 88), (-66, 64), (-42, 64), (-18, 64), (6, 64), (30, 64), (30, 40), (30, 16), (30, -8), (30, -32), (54, -32), (78, -32)]
l = [(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)]
print(l[::-1])