# 我怎样才能扭转在Python中的列表？

``array = [0,10,20,40] for (i = array.length() - 1 ;i >= 0; i--)` `

` `>>> array=[0,10,20,40] >>> for i in reversed(array): ... print(i)` `

` `>>> L = [0,10,20,40] >>> L[::-1] [40, 20, 10, 0]` `

` `>>> L = [0,10,20,40] >>> L.reverse() >>> L [40, 20, 10, 0]` `

` `>>> L[::-1] [40, 20, 10, 0]` `

` `L = [0,10,20,40] p = L[::-1] # Here p will be having reversed list` `

` `L.reverse() # Here L will be reversed in-place (no new list made)` `

` `array.reverse()` `

` `newArray = array[::-1]` `
` `for x in array[::-1]: do stuff` `

` `def reverse_in_place(lst): # Declare a function size = len(lst) # Get the length of the sequence hiindex = size - 1 its = size/2 # Number of iterations required for i in xrange(0, its): # i is the low index pointer temp = lst[hiindex] # Perform a classic swap lst[hiindex] = lst[i] lst[i] = temp hiindex -= 1 # Decrement the high index pointer print "Done!" # Now test it!! array = [2, 5, 8, 9, 12, 19, 25, 27, 32, 60, 65, 1, 7, 24, 124, 654] print array # Print the original sequence reverse_in_place(array) # Call the function passing the list print array # Print reversed list **The result:-** [2, 5, 8, 9, 12, 19, 25, 27, 32, 60, 65, 1, 7, 24, 124, 654] Done! [654, 124, 24, 7, 1, 65, 60, 32, 27, 25, 19, 12, 9, 8, 5, 2]` `

`l[::-1]`可能会比较慢，因为它在反转之前复制列表。 在`reversed(l)`迭代器周围添加`list()`调用必须增加一些开销。 当然，如果你想要一个列表或一个迭代器的副本，然后使用这些相应的方法，但是如果你想只是反转列表，那么`l.reverse()`似乎是最快的方法。

` `def rev_list1(l): return l[::-1] def rev_list2(l): return list(reversed(l)) def rev_list3(l): l.reverse() return l` `

` `l = list(range(1000000))` `

Python 3.5的时机

` `timeit(lambda: rev_list1(l), number=1000) # 6.48 timeit(lambda: rev_list2(l), number=1000) # 7.13 timeit(lambda: rev_list3(l), number=1000) # 0.44` `

Python 2.7时间

` `timeit(lambda: rev_list1(l), number=1000) # 6.76 timeit(lambda: rev_list2(l), number=1000) # 9.18 timeit(lambda: rev_list3(l), number=1000) # 0.46` `

` `list1 = [3,4,3,545,6,4,34,243] list1.reverse() list1[::-1]` `
` `array=[0,10,20,40] for e in reversed(array): print e` `

` `>>> array = [1,2,3,4] >>> for item in reversed(array): >>> print item` `

` `def reverse(a): midpoint = len(a)/2 for item in a[:midpoint]: otherside = (len(a) - a.index(item)) - 1 temp = a[otherside] a[otherside] = a[a.index(item)] a[a.index(item)] = temp return a` `

` `z = range(1000000) startTimeTic = time.time() y = z[::-1] print("Time: %ss" % (time.time() - startTimeTic)) f = range(1000000) startTimeTic = time.time() g = list(reversed(f)) print("Time: %ss" % (time.time() - startTimeTic))` `

` `Time: 0.00489711761475 s Time: 0.00609302520752 s` `

` `array=[0,10,20,40] for e in reversed(array): print e` `

`[array[n] for n in range(len(array)-1, -1, -1)]`

` `def reverse(text): output = [] for i in range(len(text)-1, -1, -1): output.append(text[i]) return output` `

` `a = [1,2,3,4] a = a[::-1] print(a) >>> [4,3,2,1]` `

` `for i in xrange(len(array) - 1, -1, -1): print i, array[i]` `

` `>>> for element in reversed(your_array): ... print element` `

` `>>> l = [1,2,3,4,5,6]; nl=[] >>> while l: nl.append(l.pop()) >>> print nl [6, 5, 4, 3, 2, 1]` `
` `def reverse(text): lst=[] for i in range(0,len(text)): lst.append(text[(len(text)-1)-i]) return ''.join(lst) print reverse('reversed')` `
` `list_data = [1,2,3,4,5] l = len(list_data) i=l+1 rev_data = [] while l>0: j=il l-=1 rev_data.append(list_data[-j]) print "After Rev:- %s" %rev_data` `
` `>>> l = [1, 2, 3, 4, 5] >>> print(reduce(lambda acc, x: [x] + acc, l, [])) [5, 4, 3, 2, 1]` `

` `>>> array = [0, 10, 20, 40] >>> [array[~i] for i, _ in enumerate(array)] [40, 20, 10, 0]` `

` `def reverse(my_list): L = len(my_list) for i in range(L/2): my_list[i], my_list[Li - 1] = my_list[Li-1], my_list[i] return my_list` `
` `>>> L = [1, 2, 3, 4] >>> L = [L[-i] for i in range(1, len(L) + 1)] >>> L [4, 3, 2, 1]` `

` `>>> l = [1,2,3,4,5,6,7] >>> for i in range(len(l)//2): ... l[i], l[-1-i] = l[-1-i], l[i] ... >>> l [7, 6, 5, 4, 3, 2, 1]` `

` `>>> l = [1,2,3,4,5] >>> for i in l.__reversed__(): ... print i ... 5 4 3 2 1 >>>` `