0

I use from Dijkstra algorithm in my code and it returns output that there is in below: How can I convert this:

deque(['[5.0, 10.0]', '[7.5, 9.0]', '[8.8, 8.48]', '[11.261467889908257, 9.956880733944955]', '[11.5, 
9.45]', '[14.4, 8.0]', '[15.47191011235955, 10.382022471910112]', '[17.0, 10.0]'])
<class 'collections.deque'>

to list of list, like this:

[[5.0, 10.0], [7.5, 9.0], [8.8, 8.48], [11.261467889908257, 9.956880733944955], [11.5, 
9.45], [14.4, 8.0], [15.47191011235955, 10.382022471910112], [17.0, 10.0]]

I want to have a list of lists that I can access to elements of the list and work with them.

There is another way to solve this problem instead of importing ast.

ZahraRezaei
  • 251
  • 2
  • 14
  • [x for x in iterable] although I'm noticing that you don't have any lists inside that particular iterable. You'll get what's actually in there. Do you really just want to ask about converting a string to python code, or parsing a string? – Kenny Ostrom Nov 15 '20 at 15:20

3 Answers3

2

I hope this helps:

In [162]: q
Out[162]:
deque(['[5.0, 10.0]',
       '[7.5, 9.0]',
       '[8.8, 8.48]',
       '[11.261467889908257, 9.956880733944955]',
       '[11.5, 9.45]',
       '[14.4, 8.0]',
       '[15.47191011235955, 10.382022471910112]',
       '[17.0, 10.0]'])

In [163]: [[float(x) for x in x.replace('\'','').replace('[','').replace(']','').split(',')] for x in q]
Out[163]:
[[5.0, 10.0],
 [7.5, 9.0],
 [8.8, 8.48],
 [11.261467889908257, 9.956880733944955],
 [11.5, 9.45],
 [14.4, 8.0],
 [15.47191011235955, 10.382022471910112],
 [17.0, 10.0]]

Another shorter and better method:

In [168]: q
Out[168]:
deque(['[5.0, 10.0]',
       '[7.5, 9.0]',
       '[8.8, 8.48]',
       '[11.261467889908257, 9.956880733944955]',
       '[11.5, 9.45]',
       '[14.4, 8.0]',
       '[15.47191011235955, 10.382022471910112]',
       '[17.0, 10.0]'])

In [169]: [ast.literal_eval(x) for x in q]
Out[169]:
[[5.0, 10.0],
 [7.5, 9.0],
 [8.8, 8.48],
 [11.261467889908257, 9.956880733944955],
 [11.5, 9.45],
 [14.4, 8.0],
 [15.47191011235955, 10.382022471910112],
 [17.0, 10.0]]
idar
  • 614
  • 6
  • 13
0

using eval() or .literal_eval() and list comprehension:

import ast
from collections import deque
q = deque(['[5.0, 10.0]', '[7.5, 9.0]', '[8.8, 8.48]', '[11.261467889908257, 9.956880733944955]', '[11.5, 9.45]', '[14.4, 8.0]', '[15.47191011235955, 10.382022471910112]', '[17.0, 10.0]'])
ans = [ast.literal_eval(x) for x in q]
print(ans)
# output 
[[5.0, 10.0], [7.5, 9.0], [8.8, 8.48], [11.261467889908257, 9.956880733944955], [11.5, 9.45], [14.4, 8.0], [15.47191011235955, 10.382022471910112], [17.0, 10.0]]
Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21
  • [Using python's eval() vs. ast.literal_eval()?](https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval#:~:text=literal_eval()%20only%20considers%20a,sets%2C%20booleans%2C%20and%20None%20.&text=literal_eval()%20will%20raise%20an,will%20happily%20delete%20your%20files.) – Tomerikoo Nov 15 '20 at 15:26
0

Try:

>>> from collections import deque
>>> d = deque(['[5.0, 10.0]', '[7.5, 9.0]', '[8.8, 8.48]', '[11.261467889908257, 9.956880733944955]', '[11.5, 9.45]', '[14.4, 8.0]', '[15.47191011235955, 10.382022471910112]', '[17.0, 10.0]'])
>>> [eval(q) for q in d]
[[5.0, 10.0],
 [7.5, 9.0],
 [8.8, 8.48],
 [11.261467889908257, 9.956880733944955],
 [11.5, 9.45],
 [14.4, 8.0],
 [15.47191011235955, 10.382022471910112],
 [17.0, 10.0]]
BioGeek
  • 21,897
  • 23
  • 83
  • 145
  • [Using python's eval() vs. ast.literal_eval()?](https://stackoverflow.com/questions/15197673/using-pythons-eval-vs-ast-literal-eval#:~:text=literal_eval()%20only%20considers%20a,sets%2C%20booleans%2C%20and%20None%20.&text=literal_eval()%20will%20raise%20an,will%20happily%20delete%20your%20files.) – Tomerikoo Nov 15 '20 at 15:26
  • If your input is untrusted user input, then you should indeed use `ast.literal_eval()`. – BioGeek Nov 15 '20 at 15:28