麻烦与for循环,.append,np.asarray

问题描述 投票:0回答:1

从excelsheet,我导入各种列,我写道:

import numpy as np
totaloutput = []

inputdata = np.stack(various columns)
for "number of variables in columns" in inputdata:
     calculate several numpy.ndarray-type arrays
     output = np.column_stack(several numpy.ndarray-type arrays)
totaloutput.append(output)

当我打印totaloutput时,我得到:

[array([['0.8', '4.0', '0.5', '5.0', 'X','Y', '16.0',
    '345.0', '285.0', '0.5843940254127079', '0.3583943421752271'],
   ['0.8', '4.0', '0.5', '5.0', 'X','Y', '17.0',
    '345.0', '285.0', '0.36329780170652354', '0.22314099222162737'],
   [etc], 
   [etc]],dtype='<U32'), array([['1.2', '4.0', '0.5', '5.0', 'X', 'Y',
   '16.0',
   '345.0', '15.0', '0.787996644827825', '0.48299132454894594'],
   [etc],
   [etc]],dtype='<U32'),

根据type(totaloutput)输出的类型是list。但是,为了能够导出数据,我必须设法获得以下形状的数据:

[['0.800000011920929' '3.5' '1.0' '4.0' 'X', 'Y', '15.0'
'345.0' '285.0' '0.6222837267695641' '0.37663730483688007']
['0.800000011920929' '3.5' '1.0' '4.0'  'X', 'Y', '15.0'
'345.0' '285.0' '1.4079677072051757' '0.8500865690052523'][etc][etc]]

我以为我解决了这个问题:

totaloutput = np.asarray(totaloutput)
totaloutput = np.reshape((totaloutput, ((len(inputdata)),11))

每当我扩展inputdata的数量(这是脚本的目标,自动计算大量数据)时,np.asarray似乎不再起作用了。我发现有人也有problems with this

我可以通过在totaloutput之前和之后打印np.asarray(totaloutput)来确认这一点,并说明两种打印都是相同的。 (不受欢迎的数组= ... dtype事物)奇怪的是,当我在np.asarray(totaloutput)之后打印type时,它确实说numpy.ndarray

使用较小的inputdata,在np.asarray(totaloutput)之后,它产生一个整洁的numpy.ndarray输出。

我已经尝试了很多其他方法来获得正确的输出,我已经看到了

for i in range(len(inputdata)):
    print(totaloutput.item(i))

正是我需要的,但每当我尝试:

for i in range(len(inputdata)):
    finaloutput = (totaloutput.item(i))

我明白了

totaloutput =(totaloutput.item(i)) AttributeError:'str'对象没有属性'item'

python list numpy for-loop append
1个回答
0
投票

我假设你的问题是你有numpy数组的列表,你想将它堆叠成一个列表?

如果是对的。尝试过np.vstack()

Numpy Example

© www.soinside.com 2019 - 2024. All rights reserved.