I want to create a 2d dimensional array in NumPy of size 128x2
and store in it long integers (each integer has 128-bit size).
我想在NumPy中创建一个大小为128x2的二维数组,并在其中存储长整数(每个整数的大小为128位)。
CODE:
码:
keys = np.zeros(shape=(128, 2))
for i in range(0, 128):
key1 = random.randrange(1 << 127, 1 << 128)
key2 = random.randrange(1 << 127, 1 << 128)
keys[i, 0] = key1
keys[i, 1] = key2
print(keys)
A random key has the following output:
随机密钥具有以下输出:
212325597117085680729458082297541802625
However when I print my keys array I got the following output:
但是,当我打印我的键阵列时,我得到以下输出:
OUTPUT:
OUTPUT:
[[ 3.24916136e+38 1.88464325e+38]
[ 3.01375156e+38 2.73451722e+38]
[ 1.85204001e+38 2.47016718e+38]
[ 1.85813038e+38 2.66817805e+38]
[ 1.93774249e+38 2.62902937e+38]
[ 2.77003163e+38 2.13490918e+38]
[ 1.92502885e+38 3.28965325e+38]
[ 3.03869869e+38 2.27308256e+38]
[ 2.97958126e+38 2.50477741e+38]
[ 1.82747542e+38 2.17062238e+38]
[ 2.59264124e+38 3.17242510e+38]
[ 2.43152125e+38 3.33742346e+38]
etc
How do I print the keys in their original format?
如何以原始格式打印密钥?
1 个解决方案
#1
0
There are two issues you're having with your code.
您的代码存在两个问题。
The first is that your using an inappropriate dtype
for your array of long integers. The default dtype
of the array returned by np.zeros
is float64
, which means you're going to be losing a lot of precision when saving your integers to that array. It doesn't matter how you go about formatting the output afterwards, the data simply isn't there any more.
首先是你为长整数数组使用了一个不合适的dtype。 np.zeros返回的数组的默认dtype是float64,这意味着在将整数保存到该数组时,您将失去很多精度。之后如何格式化输出并不重要,数据根本就不存在了。
Unfortunately, numpy
doesn't have a built-in 128-bit integer type. You'll have to decide for yourself how to store it. A composite dtype
with two uint64
s might work (though you'd have to do your own carrying between the numbers whenever you do math operations on them). Alternatively, if you don't need to do any math on the long integers in the array, you could use a string dtype
. A third option would be to use object
as the dtype
and let it use Python's normal int
s (which support unlimited precision math, but with much worse performance than hardware-supported numpy
types).
不幸的是,numpy没有内置的128位整数类型。你必须自己决定如何存储它。具有两个uint64的复合dtype可能会起作用(尽管每当你对它们进行数学运算时,你必须自己携带数字)。或者,如果您不需要对数组中的长整数进行任何数学运算,则可以使用字符串dtype。第三种选择是使用object作为dtype并让它使用Python的普通int(它支持无限精度数学,但性能比硬件支持的numpy类型差得多)。
The second issue is formatting the integers for printing (what you were actually asking about). You can call np.set_printoptions
to specify a formatting approach for your array, but this will only help if you are using a dtype
natively supported by numpy
. If you're using some other type, you may need to write a loop over the array contents and print the items out appropriately as you go.
第二个问题是格式化打印的整数(你实际问的是什么)。您可以调用np.set_printoptions为您的数组指定格式化方法,但这只有在您使用numpy本机支持的dtype时才有用。如果您正在使用其他类型,则可能需要在数组内容上编写一个循环,并在出发时适当地打印出项目。
#1
0
There are two issues you're having with your code.
您的代码存在两个问题。
The first is that your using an inappropriate dtype
for your array of long integers. The default dtype
of the array returned by np.zeros
is float64
, which means you're going to be losing a lot of precision when saving your integers to that array. It doesn't matter how you go about formatting the output afterwards, the data simply isn't there any more.
首先是你为长整数数组使用了一个不合适的dtype。 np.zeros返回的数组的默认dtype是float64,这意味着在将整数保存到该数组时,您将失去很多精度。之后如何格式化输出并不重要,数据根本就不存在了。
Unfortunately, numpy
doesn't have a built-in 128-bit integer type. You'll have to decide for yourself how to store it. A composite dtype
with two uint64
s might work (though you'd have to do your own carrying between the numbers whenever you do math operations on them). Alternatively, if you don't need to do any math on the long integers in the array, you could use a string dtype
. A third option would be to use object
as the dtype
and let it use Python's normal int
s (which support unlimited precision math, but with much worse performance than hardware-supported numpy
types).
不幸的是,numpy没有内置的128位整数类型。你必须自己决定如何存储它。具有两个uint64的复合dtype可能会起作用(尽管每当你对它们进行数学运算时,你必须自己携带数字)。或者,如果您不需要对数组中的长整数进行任何数学运算,则可以使用字符串dtype。第三种选择是使用object作为dtype并让它使用Python的普通int(它支持无限精度数学,但性能比硬件支持的numpy类型差得多)。
The second issue is formatting the integers for printing (what you were actually asking about). You can call np.set_printoptions
to specify a formatting approach for your array, but this will only help if you are using a dtype
natively supported by numpy
. If you're using some other type, you may need to write a loop over the array contents and print the items out appropriately as you go.
第二个问题是格式化打印的整数(你实际问的是什么)。您可以调用np.set_printoptions为您的数组指定格式化方法,但这只有在您使用numpy本机支持的dtype时才有用。如果您正在使用其他类型,则可能需要在数组内容上编写一个循环,并在出发时适当地打印出项目。