简体   繁体   中英

Compact form of python first and second elements of array

I have defined two arrays:

a=np.array([[2,3,4],[5,6,7],[8,9,10]])
b=np.array([-1,-2])

and created a third one:

x=np.asarray([[x - a/2, x + a/2] for x in b])

Now, I have defined two variables

u,v = x[:,0], x[:,1]

My question is extremely simple: is there a way to define those variables without the comma, using only array operations? If I write

 u,v = x[:,]

the ordering comes out wrong.

If x is 2D:

u, v = x.T

If x is ND:

u, v = np.swapaxes(x, 0, 1)

To confirm:

>>> np.all(u == x[:, 0])
True

>>> np.all(v == x[:, 1])
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM