I want to convert a numpy array to list.
input is :
s = [ 6.72599983 -7.15100002 4.68499994]
i want output as :
s = [6.72599983, -7.15100002, 4.68499994]
how I will do this in python?
I want to convert a numpy array to list.
input is :
s = [ 6.72599983 -7.15100002 4.68499994]
i want output as :
s = [6.72599983, -7.15100002, 4.68499994]
how I will do this in python?
If you have a numpy.ndarray
, then try this:
>>> import numpy
>>> lst = [6.72599983, -7.15100002, 4.68499994]
>>> numpy.asarray(lst)
array([ 6.72599983, -7.15100002, 4.68499994])
>>> list(numpy.asarray(lst))
[6.7259998300000001, -7.1510000199999997, 4.68499994]
If you have wrongly casted the numpy.array
into a string, then you need to use that cleaning trick with ast to get it into a list.
>>> import ast, numpy
>>> s = str(numpy.asarray(lst))
>>> s
'[ 6.72599983 -7.15100002 4.68499994]'
>>> list(ast.literal_eval(",".join(s.split()).replace("[,", "[")))
[6.72599983, -7.15100002, 4.68499994]
Your question still is quite unclear.
If s
is really of type 'numpy.ndarray' and not a string (as the older version of your question suggested), then just do
s = list(s)
and s
will become a list.
s.split()
should work.
You can find solution here: Python - convert string to list