如何以二进制表示模式打印 numpy 数组

use*_*783 4 python binary numpy

我有一个以下 numpy 数组

a = np.array([[1,2,3,4 ,11, 12,13,14,21,22,23,24,31,32,33,34 ]], dtype=uint8)

当我打印 ai 时得到以下输出

[[ 1  2  3  4 11 12 13 14 21 22 23 24 31 32 33 34]]
Run Code Online (Sandbox Code Playgroud)

我怎样才能得到二进制表示的输出?

例如

[[ 00000001  00000010  00000011  00000100 ...]]
Run Code Online (Sandbox Code Playgroud)

And*_*dyK 5

您可以使用numpy.binary_repr

它接受一个数字作为输入,并将其二进制表示形式作为字符串返回。您可以向量化此函数,以便它可以将数组作为输入:

np.vectorize(np.binary_repr)(a, width=8)
Run Code Online (Sandbox Code Playgroud)