【发布时间】:2018-08-12 22:16:13
【问题描述】:
从 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。
我可以通过在np.asarray(totaloutput) 之前和之后打印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))
我明白了
总输出 = (总输出.item(i))
AttributeError: 'str' 对象有 没有属性“项目”
【问题讨论】:
标签: python list numpy for-loop append