Python 一维数组到二维数组的 Numpy 列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21322564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:35:40  来源:igfitidea点击:

Numpy list of 1D Arrays to 2D Array

pythonarraysnumpy

提问by David Folkner

I have a large list files that contain 2D numpy arrays pickled through numpy.save. I am trying to read the first column of each file and create a new 2D array.

我有一个很大的列表文件,其中包含通过numpy.save. 我正在尝试读取每个文件的第一列并创建一个新的二维数组。

I currently read each column using numpy.loadwith a mmap. The 1D arrays are now in a list.

我目前使用读每一列numpy.loadmmap。一维数组现在在一个列表中。

col_list = []
for f in file_list:
    Temp = np.load(f,mmap_mode='r')
    col_list.append(Temp[:,0])

How can I convert this into a 2D array?

如何将其转换为二维数组?

采纳答案by C. Yduqoli

You can use

您可以使用

numpy.stack(arrays, axis=0)

if you have an array of arrays. You can specify the axis in case you want to stack columns and not rows.

如果你有一个数组数组。如果要堆叠列而不是行,则可以指定轴。

回答by senshin

You can just call np.arrayon the list of 1D arrays.

您可以调用np.array一维数组列表。

>>> import numpy as np
>>> arrs = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9])]
>>> arrs
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]
>>> arr2d = np.array(arrs)
>>> arr2d.shape
(3, 3)
>>> arr2d
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

回答by splendor

The array may be recreated:

可以重新创建数组:

a = np.array(a.tolist())