如何在 Python 中反转列表的一部分(切片)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4647368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I reverse a part (slice) of a list in Python?
提问by Manu
Why doesn't this work?
为什么这不起作用?
# to reverse a part of the string in place
a = [1,2,3,4,5]
a[2:4] = reversed(a[2:4]) # This works!
a[2:4] = [0,0] # This works too.
a[2:4].reverse() # But this doesn't work
采纳答案by Sven Marnach
a[2:4]creates a copy of the selected sublist, and this copy is reversed by a[2:4].reverse(). This does not change the original list. Slicing Python lists always creates copies -- you can use
a[2:4]创建所选子列表的副本,此副本由 反转a[2:4].reverse()。这不会更改原始列表。切片 Python 列表总是会创建副本——您可以使用
b = a[:]
to copy the whole list.
复制整个列表。
回答by jaydel
a[2:4] is a copy of the list a that is built using the 2,3,4 items in list a. The first two work because you are assigning the changes into those spots in the original list. The last one doesn't work because you are not affecting the original list.
a[2:4] 是使用列表 a 中的 2,3,4 项构建的列表 a 的副本。前两个工作是因为您将更改分配到原始列表中的那些位置。最后一个不起作用,因为您没有影响原始列表。
回答by dmjalund
Another way you might consider is to use a reversed slice:
您可能会考虑的另一种方法是使用反向切片:
a[2:4] = a[3:1:-1]
回答by Reman
Just use the slice and reverse it.
只需使用切片并将其反转即可。
a[2:4] = a[2:4][::-1]
回答by Arindam Roychowdhury
Here's a weird example and weirder solution using slicing and a bit of list fundamentals.
这是一个使用切片和一些列表基础知识的奇怪示例和更奇怪的解决方案。
Problem:Reverse a list in parts of two.
问题:反转列表的两部分。
I/P :[1,2,3,4,5,6]
I/P :[1,2,3,4,5,6]
O/P:[3,2,1,6,5,4]
O/P:[3,2,1,6,5,4]
Soln:
索恩:
[item for i in range(0,len(l),len(l)/2) for item in l[i:i+len(l)/2][::-1]]
Problem: Reverse the letters of someones name.
问题:反转某人姓名的字母。
E.g Harry Porter
例如哈利波特
O/p: yrraH retroP
O/p: yrrah 复古
Soln:
索恩:
' '.join(map(lambda x:x[::-1], s.split()))
回答by Albert
I believe it's worth further elaborating the reasons why
我认为值得进一步详细说明原因
a[2:4].reverse()
does not seem to work.
似乎不起作用。
In addition to the fact that a[2:4]creates a copy in the memory, it should be noted that
除了a[2:4]在内存中创建副本之外,还需要注意的是
list.reverse()
reverses the list in place and its return type is None. If you do
原地反转列表,其返回类型为None. 如果你这样做
print(a[2:4].reverse())
Noneis printed. Obviously, if you forcefully do,
None被打印。显然,如果你强行这样做,
a[2:4] = a[2:4].reverse()
Python would raise a TypeError. However, there does exist a copy of a[2:4]in the memory which has been reversed. It's just you have no reference to access it.
Python 会引发一个TypeError. 但是,内存中确实存在a[2:4]已反转的副本。只是您没有访问它的参考。
On the other hand, a[2:4] = reversed(a[2:4])works because reversed()returns a reverse iterator.
另一方面,a[2:4] = reversed(a[2:4])工作是因为reversed()返回一个反向迭代器。
See comments of the accepted answer for how a[2:4]being different when appearing on the left of assignment.
有关a[2:4]出现在作业左侧时的不同之处,请参阅已接受答案的评论。
回答by norok2
One way of doing this purely in-place is with the good old loops:
一种纯粹就地执行此操作的方法是使用良好的旧循环:
def reverse(seq, start, stop):
size = stop + start
for i in range(start, (size + 1) // 2 ):
j = size - i
seq[i], seq[j] = seq[j], seq[i]
l = list(range(10))
print(l)
reverse(l, 2, 5)
print(l)

