I will write how to exchange elements in an array in Python and how to reverse the array. Thank you.
n = [*range(1,6)]
for i in range(len(n)//2):
n[i], n[len(n)-i-1] = [len(n)-i-1], n[i]
#The beginning and the back, the second and the second from the back are exchanged.
If the goal is to reverse the array.
n = [*range(1,6)][::-1]
n = [*range(1,6)]
reverse_n = sorted(n,reverse=True)
#sorted is non-destructive, does not change n itself, and returns n in reverse order.
n = [*range(1,6)]
n.sort(reverse=True)
#sort is destructive and modifies n itself. The value returned is None.
that's all. Thank you very much.
Recommended Posts