存储整数并以矩阵形式浮动在numpy数组中

时间:2016-10-25 08:43:58

标签: python arrays numpy matrix

以下代码包含PostGIS查询并获取'product',其中包括整数和浮点数

import numpy

k = 8
for i in cor_list:
    for l in cor_list:
        cur.execute(query, (i, l, False, False))
        element = cur.fetchall()
        product = sum([a[-1] for a in element[:-1]])
        print product
        ss = numpy.array(product, ndmin = 2)
kk = ss.reshape((k,k))

部分产品如下:

0
6460.51962839
16386.3142965
18349.9662043
13071.5492165
8349.95786602
3977.69337529
10471.7888158
6460.51962839
0
9925.79466809
11889.4465759

我希望将这些产品安排在8个8的numpy数组中,看起来像,

enter image description here

但是当我运行上面的代码时,我收到了这个错误:

ValueError: total size of new array must be unchanged

如何以矩阵形式存储由numpy数组中的for循环生成的整数和浮点数?

1 个答案:

答案 0 :(得分:2)

我认为您可以通过执行以下操作来修复代码:

import numpy

product = []
k = 8
for i in cor_list:
    for l in cor_list:
        cur.execute(query, (i, l, False, False))
        element = cur.fetchall()
        product.append( sum([a[-1] for a in element[:-1]]) )
kk = numpy.array(product).reshape((k,k))