Reputation: 509
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.
Upvotes: 2
Views: 1173
Reputation: 231385
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)
Upvotes: 2
Reputation: 51155
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)]
Upvotes: 3
Reputation: 1835
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.
Upvotes: 1