1

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 a numpy array? Thanks!

2
  • How does a length-6 list contain 3 strings and 5 floats? Isn't that 8 objects? Commented Aug 31, 2015 at 6:54
  • oops, 8 objects is correct. Commented Aug 31, 2015 at 17:18

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for posting below hpaulj. Didn't know a post had come through.
1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.