反转列表项值

ael*_*ath 0 python

我想反转列表中的所有值。我的列表由 0、1、2 和 3 组成(例如 [0,2,2,3,1,3,2]),我想反转项目的值(将所有 0 更改为 3,所有1 到,所有 2 到 1,所有 3 到 0 => [3,1,1,0,2,0,1])。

在 python 中可能吗?

我尝试使用列表理解但没有成功。

li=[0,2,2,3,1,3,2]
print(list(reversed(li)))
Run Code Online (Sandbox Code Playgroud)

我拥有的:[0,2,2,3,1,3,2] 我想要的:[3,1,1,0,2,0,1]

U10*_*ard 5

使用 amap并用列表中的最大值减去每个值l

>>> l = [0,2,2,3,1,3,2]
>>> list(map(max(l).__sub__, l))
[3, 1, 1, 0, 2, 0, 1]
>>> 
Run Code Online (Sandbox Code Playgroud)

或者使用列表理解:

>>> [max(l) - i for i in l]
[3, 1, 1, 0, 2, 0, 1]
>>> 
Run Code Online (Sandbox Code Playgroud)