[英]How to return a reversed list back to its original state in python
I was just curious as to whether or not there is a function to turn a reversed list back to its original form or would the solution just to be to write the code to return it myself?我很好奇是否有一个 function 可以将反向列表转回其原始形式,或者解决方案只是编写代码以自己返回它?
I do understand that i can simply assign the list to a variable(x) prior to reversing and then just assign the list back to x, but i am just curious.我知道我可以在反转之前简单地将列表分配给变量 (x),然后将列表分配回 x,但我只是好奇。
I did think that calling the reverse() function twice would do this, but it did not,我确实认为调用 reverse() function 两次可以做到这一点,但事实并非如此,
Any answers are appreciated.任何答案表示赞赏。
my_list = ["This", "is", "an", "example", "list"]
x = my_list
my_list.reverse()
print(my_list)
my_list = x
print(my_list)
usereversed()
instead (which would return a new reversed iterable instead of reversing the list in place)使用
reversed()
代替(这将返回一个新的反向迭代而不是反转列表)
my_list = ["This", "is", "an", "example", "list"]
x = list(reversed(my_list))
print(x)
print(my_list)
output: output:
['list', 'example', 'an', 'is', 'This']
['This', 'is', 'an', 'example', 'list']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.