Converting nested lists of data into multidimensio

2019-04-07 18:13发布

In the code below I am building data up in a nested list. After the for loop what I would like is to cast it into a multidimensional Numpy array as neatly as possible. However, when I do the array conversion on it, it only seems to convert the outer list into an array. Even worse when I continue downward I wind up with dataPoints as shape (100L,)...so an array of lists where each list is my data (obviously I wanted a (100,3)). I have tried fooling with numpy.asanyarray() also but I can't seem to work it out. I would really like a 3d array from my 3d list from the outset if that is possible. If not, how can I get the array of lists into a 2d array without having to iterate and convert them all?

Edit: I am also open to better way of structuring the data from the outset if it makes processing easier. However, it is coming over a serial port and the size is not known beforehand.

import numpy as np
import time

data = []
for _i in range(100):   #build some list of lists
    d = [np.random.rand(), np.random.rand(), np.random.rand()]
    data.append([d,time.clock()])

dataArray = np.array(data)  #now I have an array of lists of a list(of data) and a time
dataPoints = dataArray[:,0] #this is the data in an array of lists

2条回答
我只想做你的唯一
2楼-- · 2019-04-07 18:25

dataPoints is not a 2d list. Convert it first into a 2d list and then it will work:

d=np.array(dataPoints.tolist())

Now d is (100,3) as you wanted.

查看更多
Evening l夕情丶
3楼-- · 2019-04-07 18:44

If a 2d array is what you want:

from itertools import chain
dataArray = np.array(list(chain(*data)),shape=(100,3))

I didn't work out the code so you may have to change the column/row ordering to get the shape to match.

查看更多
登录 后发表回答