David Folkner - 4 months ago 41

Python Question

I have a large list files that contain 2D numpy arrays pickled through

`numpy.save`

I currently read each column using

`numpy.load`

`mmap`

`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?

Answer

You can just call `np.array`

on the list of 1D arrays.

```
>>> 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]])
```

Source (Stackoverflow)

Comments