Issue
I want to convert the array A
containing string elements to list. But I am running into error while using split()
. I present the expected output.
import numpy as np
from numpy import nan
A=np.array(['[1]', '[2]', '[3]', '[4]', '[5]'])
A.split()
print(A)
The error is
in <module>
A.split()
AttributeError: 'list' object has no attribute 'split'
The expected output is
array([[1], [2], [3], [4], [5]])
Solution
I think this should work
A=np.array(['[1]', '[2]', '[3]', '[4]', '[5]'])
output = [[int(a.strip("[|]"))] for a in A]
print(output)
[[1], [2], [3], [4], [5]]
if you prefer just an list, instead of a list of list
A=np.array(['[1]', '[2]', '[3]', '[4]', '[5]'])
output = [int(a.strip("[|]")) for a in A]
print(output)
[1, 2, 3, 4, 5]
Answered By - Lucas M. Uriarte
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.