I have a numpy array which looks like
a = ['blue' 'red' 'green']
and I want it to become
b = ['blue', 'red', 'green']
I tried
b = a.split(' ')
but it returns an error: 'numpy.ndarray' object has no attribute 'split'
I have a numpy array which looks like
a = ['blue' 'red' 'green']
and I want it to become
b = ['blue', 'red', 'green']
I tried
b = a.split(' ')
but it returns an error: 'numpy.ndarray' object has no attribute 'split'
Simply turn it to a list:
a = numpy.array(['blue', 'red', 'green'])
print a
>> ['blue' 'red' 'green']
b = list(a)
print b
>> ['blue', 'red', 'green']
But why would you have a numpy array with strings?
I had a similar problem with a list without commas and with arbitrary number of spaces. E.g.:
[2650 20 5]
[2670 5]
[1357 963 355]
I solved it this way:
np.array(re.split("\s+", my_list.replace('[','').replace(']','')), dtype=int)
From the console:
>>> import numpy as np
>>> import re
>>> my_list = '[2650 20 5]'
>>> result = np.array(re.split("\s+", my_list.replace('[','').replace(']','')), dtype=int)
>>> result
array([2650, 20, 5])
>>>