简体   繁体   English

如何在不修改 Python 中的原始列表的情况下反转列表

[英]How to reverse a list without modifying the original list in Python

I'm trying to use the reverse() method in the following way:我正在尝试通过以下方式使用 reverse() 方法:

>>> L=[1,2,3]
>>> R=L
>>> L.reverse()
>>> L
[3, 2, 1]
>>> R
[3, 2, 1]

Why does it reverse R as well?为什么它也会反转 R? How do I keep the original list and create a reversed on?如何保留原始列表并创建反向列表?

Thanks!谢谢!

you need to make a copy of your list你需要复制一份你的清单

L=[1,2,3]
# R=L
R = L[:]
L.reverse()

or more directly (reversing using slice notation):或更直接(使用切片符号反转):

R = L[::-1]

if you just write R = L then R is just a new reference on the same list L .如果你只是写R = L那么R只是同一个列表L上的一个新引用。

if you also need to copy the elements in your list, use copy.deepcopy ;如果您还需要复制列表中的元素,请使用copy.deepcopy R = L[:] only produces a shallow copy (which is fine in your case where there are only int s in the list). R = L[:]只产生一个浅拷贝(这在你的情况下很好,列表中只有int s)。

To avoid reversing R , you need a copy of L为避免反转R ,您需要一份L的副本

To make a copy, change要复制,请更改

R=L    <---- this is not copying L in to R

to

R= L[:]    <---- this will create a copy of L in to R
>>> L=[1,2,3]
>>> R=list(reversed(L))
>>> R
[3, 2, 1]
>>> L
[1, 2, 3]

This will do这会做

R = L[:]

Read about copying and deep copying to understand why this is happening.阅读有关复制和深度复制的内容以了解发生这种情况的原因。 In short, it is because when you do R = L , both point to the "same" list in memory so reversing one will reverse the other.简而言之,这是因为当您执行R = L ,两者都指向内存中的“相同”列表,因此反转一个将反转另一个。

Alternately you can use deepcopy或者,您可以使用deepcopy

The operation操作

R=L

means that the variable R stores whatever L is storing ie R references to whatever value L is refering.意味着变量 R 存储 L 正在存储的任何内容,即 R 对 L 所引用的任何值的引用。 Now, when you reverse L, it means the list L is refering to is reversed.As R is also refereing same address, you are seeing R as reversed.现在,当您反转 L 时,这意味着 L 所引用的列表已反转。由于 R 也引用了相同的地址,因此您将看到 R 已反转。

You have been bitten by the fact that Python assigns references, not copies.您已经被 Python 分配引用而不是副本这一事实所困扰。

Python uses reference exclusively in order to prevent unneeded copies. Python 仅使用引用来防止不需要的副本。 It uses references also when passing function arguments, and even with default function arguments.它在传递函数参数时也使用引用,甚至使用默认函数参数。 It only makes a copy when you explicitly tell it to, eg by doing它仅在您明确告诉它时才会复制,例如通过执行

L = list(R)

or或者

from copy import copy
L = copy(R)

The difference between the two is that list(R) always returns a list, where copy returns an object of the same type as the original.两者的区别在于list(R)总是返回一个列表,其中copy返回一个与原始类型相同的对象。 Also, copy does not work with iterators, but list does.此外, copy不适用于迭代器,但list可以。

Note that these are 'shallow' copies.请注意,这些是“浅”副本。 If the list contains objects, the new list contains references to the same objects.如果列表包含对象,则新列表包含对相同对象的引用。 For 'deep' copies, it has the function deepcopy in the module copy .对于“深度”副本,它在模块copy具有deepcopy功能。

from copy import deepcopy
L = deepcopy(R)

But in practise there is rarely a need to make explicit copies.但在实践中很少需要制作显式副本。 Python provides eg the reversed built-in that can be used to iterate through the list. Python 提供了例如可用于遍历列表的reversed内置函数。

for l in reversed(L):
    # do stuff

It returns an iterator, so it is efficient.它返回一个迭代器,所以它是高效的。 No need to duplicate the list.无需复制列表。 Other cases where a copy might be needed can be handled by eg generator functions.其他可能需要副本的情况可以由例如生成器函数处理。 A valid case for a copy might be when pasing data past thread boundaries.复制的有效情况可能是将数据传递到线程边界之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何在不反转原始字典的情况下反转新字典中的列表 - Python: How to reverse a list in a new dictionary without reversing the original 在不修改python中的原始列表的情况下创建列表的新修改版本 - making a new modified version of a list without modifying the original list in python Python:如何仅使用while循环返回列表而不修改原始列表? - Python: How to return a list without modifying the original list using a while loop only? 如何比较字符串列表中的反向字符串与python中的原始字符串列表? - How to compare reverse strings in list of strings with the original list of strings in python? 如何在Python中不使用reversed()或[::-1]来反转字符串列表 - How to reverse a list of strings without reversed() or [::-1] in Python 在不修改原始输入的情况下对复杂字符串列表进行详尽搜索 - exhaustive search over a list of complex strings without modifying original input 修改列表以复制另一个列表而不复制原始列表 - Modifying a list to copy another without making a copy of the original 如何在保持原始链表不变的情况下反转链表 - How to reverse a linked list while keeping the original linked list unchanged 如何使用生成器在 Python 中生成没有“反向重复”的列表的排列 - How to generate permutations of a list without “reverse duplicates” in Python using generators 如何在不使用循环的情况下反转Python中列表的顺序? - How can one reverse the order of a list in Python without using a loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM