How do I reverse a part (slice) of a list in Python?

前端 未结 7 772
灰色年华
灰色年华 2020-11-27 19:32

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]             # Thi         


        
相关标签:
7条回答
  • 2020-11-27 19:51

    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)
    
    0 讨论(0)
  • 2020-11-27 19:52

    Just use the slice and reverse it.

    a[2:4] = a[2:4][::-1]
    
    0 讨论(0)
  • 2020-11-27 19:52

    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.

    0 讨论(0)
  • 2020-11-27 19:59

    Another way you might consider is to use a reversed slice:

    a[2:4] = a[3:1:-1]
    
    0 讨论(0)
  • 2020-11-27 20:12

    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

    b = a[:]
    

    to copy the whole list.

    0 讨论(0)
  • 2020-11-27 20:12

    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

    list.reverse()
    

    reverses the list in place and its return type is None. If you do

    print(a[2:4].reverse())
    

    None is printed. Obviously, if you forcefully do,

    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.

    On the other hand, a[2:4] = reversed(a[2:4]) works because reversed() returns a reverse iterator.

    See comments of the accepted answer for how a[2:4] being different when appearing on the left of assignment.

    0 讨论(0)
提交回复
热议问题