简体   繁体   English

如何将反向列表返回到 python 中的原始 state

[英]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.

相关问题 如何在python中将列表返回到其原始状态? - how to return a list to its original state in python? 脚本完成后如何让键盘恢复原来的state? - How to return the keyboard to its original state after the completion of the script? Python:按大小(反转)对列表列表进行排序并存储原始索引 - Python: Sort list of lists by size (reversed) and store the original indices 想知道如何在更改之前返回带有原始元组的列表 - wondering how to return a list with original tuple before its changed 如何在Python列表中将单词反令牌化回原始形式 - How to detokenize words back to the original form in a list in Python 为什么这个反向列表python代码返回反向列表? - Why does this reverse list python code return the reversed list? 回溯解决方案中的变量值不会回到其原始 state - variable value in a backtracking solution is not going back to its original state 如何在Python中不使用reversed()或[::-1]来反转字符串列表 - How to reverse a list of strings without reversed() or [::-1] in Python 替换列表回到python中的原始列表 - replaced list back to original list in python 在python中将一个列表或列表列表进行字符串化并将其转换回原始形式的问题 - issue in stringifying a list or list of list in python and converting it back to original form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM