2 回答
TA贡献1898条经验 获得超8个赞
给定以下array_funct函数和filenames列表:
def array_funct(filename):
# Fake random data, replace with data read from file
data_read = [[1,2,3,4], [5,6,7,8]] # [random.sample(range(1, 10), 7), random.sample(range(1, 10), 7)]
data = np.array(data_read)
return data
filenames = ['file1.txt', 'file2.txt']
试试这个代码:
lx = [list(array_funct(file)[0]) for file in filenames]
ly = [list(array_funct(file)[1]) for file in filenames]
或者通过一次读取和滚动文件来更有效地:
all_data = [(list(arr[0]),list(arr[1])) for arr in [array_funct(f) for f in filenames]]
lx, ly = list(map(list, zip(*all_data)))
在这两种情况下,输出如下:
# lx = [[1, 2, 3, 4], [1, 2, 3, 4]]
# ly = [[5, 6, 7, 8], [5, 6, 7, 8]]
TA贡献1803条经验 获得超3个赞
就像是:
x_tot = np.array([array_funct(file[i])[:,0] for i in range(0,number_of_files)])
应该管用。
添加回答
举报