Converting a 1D numpy array to a list of lists

arrays, list, numpy, python

Solution

Using list comprehension, `str.split`:

>>> from numpy import array
>>> a = array(['java database servlets derby', 'java graphics groovy awt basic',
...            'java lucene', 'javascript android',
...            'iphone ios ipad file uiimage',
...            'javascript jquery transition effect'])
>>> list_of_lists = [x.split() for x in a]
>>> list_of_lists
[['java', 'database', 'servlets', 'derby'],
 ['java', 'graphics', 'groovy', 'awt', 'basic'],
 ['java', 'lucene'],
 ['javascript', 'android'],
 ['iphone', 'ios', 'ipad', 'file', 'uiimage'],
 ['javascript', 'jquery', 'transition', 'effect']]

Problem

I want to split a 1D numpy array into a list of lists, but I am not sure how I could do that. Basically I am dealing with an array that is filled with tags: ``` array(['java database servlets derby', 'java graphics groovy awt basic', 'java lucene', ..., 'javascript android', 'iphone ios ipad file uiimage', 'javascript jquery transition effect'], dtype=object) ``` with shape: ``` (5000L,) ``` As you can see every row contains tags separated by white-spaces. I want to store every row as a list with all the tags as separate elements and combine those lists into a list of lists. The result should look like this then: ``` list_of_lists = [["tag","tag","tag"],["tag","tag","tag"]...] ``` How could I achieve this? And if you guys know a better method to achieve what I want (namely a data structure where I can access every tag as an element of the specified row) I would be glad to hear it. Thanks in advance.

Original source