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

标签 python numpy binary

我有一个下面的 numpy 数组

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

当我打印 a 时,我得到以下输出

[[ 1  2  3  4 11 12 13 14 21 22 23 24 31 32 33 34]]

如何获得二进制表示形式的输出?

例如

[[ 00000001  00000010  00000011  00000100 ...]]

最佳答案

您可以使用 numpy.binary_repr

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

np.vectorize(np.binary_repr)(a, width=8)

关于python - 如何以二进制表示模式打印 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124694/

相关文章:

python /R : generate dataframe from XML when not all nodes contain all variables?

python - 如何匹配元音?

python - 如何在 numpy ndarray 中找到最频繁的字符串元素?

python - 绘制租金图

将二进制数转换为十进制数的 Java 程序。输入是一串零和一

python - 如何使用固定列表或数组大小作为值的 defaultdict 或字典

python - 绘制一个圆-matplotlib

python - 如何在Python中有效地将子字典转换为矩阵

c++ - 二进制表示中数字的偶数和奇数位置上的 1 的个数是多少?

c - 为什么我无法在十进制到二进制转换中获得正确的输出?