-1

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?

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
sam
  • 18,509
  • 24
  • 83
  • 116
  • Doesn't look *too* hard ... have you made any attempts? – mgilson Jan 08 '14 at 07:22
  • yes it has type – sam Jan 08 '14 at 07:23
  • possible duplicate of [Converting string that looks like a list into a real list - python](http://stackoverflow.com/questions/20250396/converting-string-that-looks-like-a-list-into-a-real-list-python) – alvas Jan 08 '14 at 07:24
  • 1
    Then you don't have a `string`, you have a numpy array -- to get a list, simply use `list(s)`. But if you thought you had a string, maybe you don't need to convert it... arrays are pretty good too :) – mgilson Jan 08 '14 at 07:24
  • its not duplicate. please dt downvote unnecessarily – sam Jan 08 '14 at 07:25
  • possible duplicate of [Python - convert string to list](http://stackoverflow.com/questions/8266529/python-convert-string-to-list) – Bakuriu Jan 08 '14 at 07:25
  • can you check my question properly first? – sam Jan 08 '14 at 07:26
  • It's not a duplicate, but you are also not asking the actual question you're asking. You're not asking how to convert a string to a list, but a numpy.ndarray to a list, which is extremely trivial. – Slater Victoroff Jan 08 '14 at 07:27
  • oh ok i may have made mistake asking question properly but inside i wrote properly. – sam Jan 08 '14 at 07:28
  • Okay, here. Please just try printing it in a console session and show the result to us. – aIKid Jan 08 '14 at 07:28
  • type is : – sam Jan 08 '14 at 07:29
  • 3
    @sam **You** should write the question properly. If *s* is `numpy.ndarray` why the *hell* do you write that it is a string and make up the code in the question to make sure to hide this? -1 -- learn to *at least* search [the documentation](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tolist.html) before asking a question. – Bakuriu Jan 08 '14 at 07:29
  • @Bakuriu : aha ok i made mistake their. sorry i wil update – sam Jan 08 '14 at 07:31
  • @sam Can you include the result of `print s`? – aIKid Jan 08 '14 at 07:33
  • possible duplicate of http://stackoverflow.com/questions/1966207/converting-numpy-array-into-python-list-structure – alvas Jan 08 '14 at 07:49

3 Answers3

3

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]
alvas
  • 115,346
  • 109
  • 446
  • 738
2

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.

Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56
0

s.split() should work.

You can find solution here: Python - convert string to list

Community
  • 1
  • 1
Amit Shakya
  • 1,396
  • 12
  • 27