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]])
CodePudding user response:
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]