Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
2D numpy.array之和的Python错误行为_Python_Python 3.x_Numpy_Numpy Ndarray - Fatal编程技术网

2D numpy.array之和的Python错误行为

2D numpy.array之和的Python错误行为,python,python-3.x,numpy,numpy-ndarray,Python,Python 3.x,Numpy,Numpy Ndarray,所以,我有一个数字列表 weird_list = [800153196, 946067665, 827629917, 868941741, 875745873, 926109150, 1353347195, 1003235074, 1053666891, 14421

所以,我有一个数字列表

weird_list = [800153196,
             946067665,
             827629917,
             868941741,
             875745873,
             926109150,
             1353347195,
             1003235074,
             1053666891,
             1442194993,
             1924716858,
             1060724069,
             1182240731,
             1646547575,
             1215762661,
             1520107722,
             1512568609,
             1534064291,
             1549459216,
             1773697582,
             1853820087,
             1696910852,
             1563415785,
             1692314635,
             1627783113]
我的目标是获得该列表每对的和的2dnp.array

例如:

weird_list = [1, 2, 3]
resulting_array = [[0, 1, 2],
                   [1, 2, 3],
                   [2, 3, 4]]
我编写了这个函数,它可以很好地用于较小的数字,但不是必需的,因为我在具有较大数字的数组上进行了测试。这个数组的问题是,我以某种方式获得了负数

我还写了一些小/大数字列表的例子

low_list = [0, 1, 2, 3]
ex_list = []
weird_list_div_10 = []
weird_list_mult_10 = []

for i in range(len(weird_list)):
    ex_list.append(i)
    weird_list_div_10.append(weird_list[i] // 10)
    weird_list_mult_10.append(weird_list[i] * 10)
完整代码的源代码:


对于原始列表中的值,uint32将起作用。顶部位不表示无符号整数的符号。

如果使用numpy int32数组,某些结果将溢出。在这些情况下,结果可能是负面的。int64不应该溢出。检查数组的数据类型
low_list = [0, 1, 2, 3]
ex_list = []
weird_list_div_10 = []
weird_list_mult_10 = []

for i in range(len(weird_list)):
    ex_list.append(i)
    weird_list_div_10.append(weird_list[i] // 10)
    weird_list_mult_10.append(weird_list[i] * 10)
import numpy as np

weird_list = [ 926109150, 1353347195, 1003235074 ]             

arr = np.array( weird_list, dtype = np.int32 )   # int32 forced here

arr.reshape( 1, -1 ) + arr.reshape( -1, 1 ) 

# Out[12]:      # int32 leads to some negative answers.
# array([[ 1852218300, -2015510951,  1929344224],
#        [-2015510951, -1588272906, -1938385027],
#        [ 1929344224, -1938385027,  2006470148]], dtype=int32)

2**31-1
# Out[14]: 2147483647  # Any number greater than this in int32 will be truncated to it's 
# 32 lowest bits.  if bit 31 ( counting bits 0 to 31 ) is 1 it's treated as a negative number.

arr = np.array( weird_list, dtype = np.int64 )   # int64  forced.

arr.reshape( 1, -1 ) + arr.reshape( -1, 1 ) 

# Out[16]:   # int64 doesn't overflow therefore all results are positive with these values
# array([[1852218300, 2279456345, 1929344224],
#        [2279456345, 2706694390, 2356582269],
#        [1929344224, 2356582269, 2006470148]])