如果要反转列表,请执行以下操作:
# Solution 1
alist = random.sample(range(1, 100), 5)
otherlist = list(reversed(alist))
# Solution 2 (My favourite)
otherlist = alist[::-1]
# Solution 3
otherlist = alist[:]
otherlist.reverse()
# Solution 4
otherlist = []
for i in range(len(alist)-1, -1, -1):
otherlist.append(alist[i])
# Solution 5
counter = count2 = 0
alist, templist = [], []
while counter<5:
alist.append(random.randint(5,10))
print(alist[counter])
counter += 1
while counter > 0:
counter -= 1
templist.append(alist[counter])
print(alist[counter])
print("Real",templist[count2])
count2 += 1