我正在阅读数据并希望制作一个二维数组。我有九个独立的数据集,在我读完所有数据集之后,我想要一个9Xwhatever数组。现在,当我要求tmax的形状时,它在循环中给出了九个单独的列表,当我在循环外时,只有一个列表中的一个数据集。如何将它们全部合并为二维数组?
import numpy as np
citydata = ['bcm2.a2.USC00101022.tmax.1960.2099.txt','bcm2.a2.USC00362682.tmax.1960.2099.txt','bcm2.a2.USC00415411.tmax.1960.2099.txt',
'ccsm.a2.USC00101022.tmax.1960.2099.txt','ccsm.a2.USC00362682.tmax.1960.2099.txt','ccsm.a2.USC00415411.tmax.1960.2099.txt',
'pcm.a2.USC00101022.tmax.1960.2099.txt','pcm.a2.USC00362682.tmax.1960.2099.txt','pcm.a2.USC00415411.tmax.1960.2099.txt']
for x in range(0,len(citydata),1):
data = np.genfromtxt(citydata[x], skip_header=1)
tmax = (data[:,3]*(9./5.))+32.
答案 0 :(得分:0)
怎么样?
np.asanyarray([(np.genfromtxt(item, skip_header=1)[:,3]*(9./5.))+32 for item in citydata])
你的文件长度都一样吗?
答案 1 :(得分:0)
尝试
import numpy as np
citydata = ['bcm2.a2.USC00101022.tmax.1960.2099.txt','bcm2.a2.USC00362682.tmax.1960.2099.txt','bcm2.a2.USC00415411.tmax.1960.2099.txt',
'ccsm.a2.USC00101022.tmax.1960.2099.txt','ccsm.a2.USC00362682.tmax.1960.2099.txt','ccsm.a2.USC00415411.tmax.1960.2099.txt',
'pcm.a2.USC00101022.tmax.1960.2099.txt','pcm.a2.USC00362682.tmax.1960.2099.txt','pcm.a2.USC00415411.tmax.1960.2099.txt']
tmax = []
for cd in citydata:
data = np.genfromtxt(citydata[x], skip_header=1)
tmax.append((data[:,3]*(9./5.))+32.)