简体   繁体   中英

Numerically append binary numbers in np.array

Considering the following example array :

a = np.array([0,1,1,0,1,1,1,0,1,0])

Which could be of any dtype (int, float...)

How would I get the following output without using nasty loops and string casts ?

np.array([0b01,0b10,0b11,0b10,0b10])
a = a.astype(int)
output = a[0::2] * 2 + a[1::2]

Gives the array you've described (though it doesn't print in binary).

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