array = [1, 2, 3, 4, 5, 6,7, 8]inverse = [] #create container for inverse arraylength = len(array) #to iterate later, returns 8counter = length - 1 #because the 8th element is on position 7 (as python starts from 0)
for i in range(length):inverse.append(array[counter])counter -= 1print(inverse)
def reverse(seq):for x in range(len(seq), -1, -1): #Iterate through a sequence starting from -1 and increasing by -1.yield seq[x] #Yield a value to the generator
def rev_manual_pos_gen(mylist):max_index = len(mylist) - 1return [ mylist[max_index - index] for index in range(len(mylist)) ]
def rev_manual_neg_gen(mylist):## index is 0 to 9, but we need -1 to -10return [ mylist[-index-1] for index in range(len(mylist)) ]
def rev_manual_index_loop(mylist):a = []reverse_index = len(mylist) - 1for index in range(len(mylist)):a.append(mylist[reverse_index - index])return a
def rev_manual_loop(mylist):a = []reverse_index = len(mylist)for index, _ in enumerate(mylist):reverse_index -= 1a.append(mylist[reverse_index])return a
loops_to_test = 100000number_of_items = 10list_to_reverse = list(range(number_of_items))if number_of_items < 15:print("a: {}".format(list_to_reverse))print('Loops: {:,}'.format(loops_to_test))# List of the functions we want to test with the timer, in print orderfcns = [rev_in_place, reversed_iterator, rev_slice, rev_copy_reverse,reversed_with_list, rev_manual_pos_gen, rev_manual_neg_gen,rev_manual_index_loop, rev_manual_loop]max_name_string = max([ len(fcn.__name__) for fcn in fcns ])for fcn in fcns:a = copy(list_to_reverse) # copy to start fresh each loopout_str = ' | out = {}'.format(fcn(a)) if number_of_items < 15 else ''# Time in ms for the given # of loops on this fcntime_str = time_str_ms(timeit(lambda: fcn(a), number=loops_to_test))# Get the output string for this functionfcn_str = '{}(a):'.format(fcn.__name__)# Add the correct string length to accommodate the maximum fcn nameformat_str = '\{\{fx:{}s}} \{\{time}}\{\{rev}}'.format(max_name_string + 4)print(format_str.format(fx=fcn_str, time=time_str, rev=out_str))
a: [0, 1]Loops: 100,000rev_in_place(a): 24.70 ms | out = [1, 0]reversed_iterator(a): 30.48 ms | out = <list_reverseiterator object at 0x0000020242580408>rev_slice(a): 31.65 ms | out = [1, 0]rev_copy_reverse(a): 63.42 ms | out = [1, 0]reversed_with_list(a): 48.65 ms | out = [1, 0]rev_manual_pos_gen(a): 98.94 ms | out = [1, 0]rev_manual_neg_gen(a): 88.11 ms | out = [1, 0]rev_manual_index_loop(a): 87.23 ms | out = [1, 0]rev_manual_loop(a): 79.24 ms | out = [1, 0]
列表中有10个项目:
rev_in_place(a): 23.39 ms | out = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]reversed_iterator(a): 30.23 ms | out = <list_reverseiterator object at 0x00000290A3CB0388>rev_slice(a): 36.01 ms | out = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]rev_copy_reverse(a): 64.67 ms | out = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]reversed_with_list(a): 50.77 ms | out = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]rev_manual_pos_gen(a): 162.83 ms | out = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]rev_manual_neg_gen(a): 167.43 ms | out = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]rev_manual_index_loop(a): 152.04 ms | out = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]rev_manual_loop(a): 183.01 ms | out = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
The cute Solution
class List_reverse():def reverse_list(self):list = [1, 2, 3, 4, 5, 6]list1 = []for i in range(1, len(list)+1):list1.append(list[-i])print(list1)
if __name__ == "__main__":obj = List_reverse()obj.reverse_list()enter code here