Converting list of lists to numpy array with multiple data types

后端 未结 2 481
傲寒
傲寒 2021-01-16 10:19

I have a list of lists I\'ve read from a file. Each of the inner lists is six elements in length, and has 3 strings and 5 floats. How do I convert this list of lists into

相关标签:
2条回答
  • 2021-01-16 11:02

    I had the same problem, but tuples are no solution. So I found (python 3.7.1):

    ll = [['one','two',1,1.23],['four','five',4,34.3],['six','seven',4,34.3]]
    
    np.array(ll, dtype = 'object')
    

    result:

    array([['one', 'two', 1, 1.23],
       ['four', 'five', 4, 34.3],
       ['six', 'seven', 4, 34.3]], dtype=object)
    
    0 讨论(0)
  • 2021-01-16 11:07

    You want a structured array, one that has a compound dtype:

    A sample list of lists:

    In [4]: ll = [['one','two',1,1.23],['four','five',4,34.3],['six','seven',4,34.3]]
    

    trying to make a regular array, produces an array of strings:

    In [5]: np.array(ll)
    Out[5]: 
    array([['one', 'two', '1', '1.23'],
           ['four', 'five', '4', '34.3'],
           ['six', 'seven', '4', '34.3']], 
           dtype='|S5')
    

    But if I specify a dtype that contains 2 strings, and int and a float, I get a 1d structured array:

    In [8]: np.array([tuple(x) for x in ll],dtype='S5,S5,i,f')
    Out[8]: 
    array([('one', 'two', 1, 1.2300000190734863),
           ('four', 'five', 4, 34.29999923706055),
           ('six', 'seven', 4, 34.29999923706055)], 
          dtype=[('f0', 'S5'), ('f1', 'S5'), ('f2', '<i4'), ('f3', '<f4')])
    

    Note that I had to convert the inner lists to tuples. That's how a structured array takes its input, and also how it displays it. It helps distinguish the structured 'row' from the uniform 'row' of a regular (2d) array.

    This the same sort of structured array that genfromtxt or loadtxt produces when reading from a csv file.

    There are other ways of specifying the dtype, and a couple of other ways of loading the data into such an array. But this is a start.


    Further testing, https://stackoverflow.com/a/47774915/901925, shows that this tuple conversion is not that time consuming. Simply creating the array takes more time.

    0 讨论(0)
提交回复
热议问题