Home > other >  Python array reverse output problem
Python array reverse output problem

Time:11-23

If __name__=="__main__ ':
A=,6,5,4,1 [9]
N=len (a)
Print a
For I in range (len (a)/2) :
A [I], a [N - I - 1)=a [N - I - 1], a [I]
Print a


A [I], a [N - I - 1)=a [N - I - 1], a [I]
This line of how to understand?

CodePudding user response:

Answer before, first take a look at a, b=b, the meaning of this sentence, a
It is to put the two variables value exchange, see the code below:
 a, b=2, 3 
Print (" before: a=", a, b="", b)
A, b=b, a
Print (" after: a=", a, b="", b)

Python can give a few a variable assignment, as the above
A, b=2, 3
A statement to the two variable assignment,
A, b=b, a is a, b values of exchange,
The same thing as in the c/c + + :
Temp=a;
A=b;
B=temp.

Back to your question:
A [I], a [N - I - 1)=a [N - I - 1], a [I]
Is the array of exchange, the first and the last one is a [0] and a [N - 1) exchange
The second and the last but one, namely a [1] and a [N - 2] exchange, and so on, until the middle of that value, thus the reverse the

CodePudding user response:

 
The import numpy as np
A=np. Array (,4,5,4,1 [9])
Print (a, np. Flip (a))
# out: [1] 9, 4, 5 4 [1, 4, 5 4 9]

CodePudding user response:

 if __name__=="__main__ ': 
X, y=1, 2
X, y=y, x
Print (x, y) 2 # 1

CodePudding user response:

The simple numerical exchange
  • Related