2

I am trying to convert list of lists and some other number with in the lists to numpy array.

So far i have tried this but still the list carries over to the array:

ll = [['119', '222', '219', '293'], '4', ['179', '124', '500', '235'], '7']
arrays = np.array(ll)

The output is:

array([list(['119', '222', '219', '293']), '4', list(['179', '124', '500', '235']), '7'], dtype=object)

My desired output is something like this:

[(array([ 119, 222, 219, 293]), 4), (array([ 179,  124, 500, 235]), 7)]

Is there a way to do this. I have been trying to get this for the last two days.

3
  • 1
    Numpy doesn't handle jagged arrays well, and really, you shouldn't try. It will mess things up if you try to do any actual numpy operations Commented Jul 27, 2018 at 19:53
  • 3
    Do you actually want a numpy array? Or do you want a list of tuples where the first element is a numpy array? Commented Jul 27, 2018 at 19:55
  • 1
    A list of tuples will do it. Commented Jul 27, 2018 at 19:57

3 Answers 3

3

Since you want to group every two elements as a tuple, and then convert the first element of each tuple to a numpy array, you can use a list comprehension with zip:

[(np.array(i, dtype=int), int(j)) for i, j in zip(ll[::2], ll[1::2])]

# Result
[(array([119, 222, 219, 293]), 4), (array([179, 124, 500, 235]), 7)]

Notice that I specify a dtype in the numpy array constructor to cast the array to integers.

If you're concerned about making two copies of the list here, you can also simply use range based indexing:

[(np.array(ll[i], dtype=int), int(ll[i+1])) for i in range(0, len(ll), 2)]
Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what i wanted. Casting the datatype into integers was what i wanted even though i didn't ask explicitly. Thanks
I would also recommend looking at hpaulj's solution, because this type of grouping is usually handled using a structured array in numpy, and learning how to leverage those is a good skill to have.
Thanks. It is indeed a good skill set to have and will spend some time understanding it.
2

You could make a structured array:

In [96]: ll = [['119', '222', '219', '293'], '4', ['179', '124', '500', '235'], '7']

In [97]: dt = np.dtype('4i,i')

In [98]: arr = np.zeros(2, dtype=dt)

In [99]: arr
Out[99]: 
array([([0, 0, 0, 0], 0), ([0, 0, 0, 0], 0)],
      dtype=[('f0', '<i4', (4,)), ('f1', '<i4')])

In [100]: arr['f0']=ll[::2]

In [101]: arr['f1']=ll[1::2]

In [102]: arr
Out[102]: 
array([([119, 222, 219, 293], 4), ([179, 124, 500, 235], 7)],
      dtype=[('f0', '<i4', (4,)), ('f1', '<i4')])

and extracted out to a list:

In [103]: arr.tolist()
Out[103]: 
[(array([119, 222, 219, 293], dtype=int32), 4),
 (array([179, 124, 500, 235], dtype=int32), 7)]

Or a 2x2 object dtype array:

In [104]: np.array(arr.tolist(),dtype=object)
Out[104]: 
array([[array([119, 222, 219, 293], dtype=int32), 4],
       [array([179, 124, 500, 235], dtype=int32), 7]], dtype=object)

In [105]: _.shape
Out[105]: (2, 2)

Comments

1

It looks like you want individual elements to be numpy arrays, not the whole thing. So you'll have to assign those particular elements directly:

ll[0][0] = np.array(ll[0][0])
ll[0][2] = np.array(ll[0][2])

You could also loop through and find "lists" and then convert them if you don't want to write individual lines.

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.