0

I have a list of one string that looks like this:

['(0.027725, 0.0088202301), (0.00055000000000000003, 0.0040760101), 
  (0.1666, 0.0020067799), (0.00545, 0.021263899)']

But I want it to be a list of tuples that look that this:

[(0.027725, 0.0088202301),
 (0.00055000000000000003, 0.0040760101),
 (0.1666, 0.0020067799),
 (0.00545, 0.021263899)]

Does anyone know how to do this?

  • 3
    You can use `ast.literal_eval` with some string manipulation, but how did this string come to be? It sounds like you should fix *that* issue if possible – juanpa.arrivillaga Apr 04 '18 at 01:01

1 Answers1

0

You can use ast.literal_eval:

import ast
s = ['(0.027725, 0.0088202301), (0.00055000000000000003, 0.0040760101), (0.1666, 0.0020067799), (0.00545, 0.021263899)']
new_s = ast.literal_eval('[{}]'.format(s[0]))

Output:

[(0.027725, 0.0088202301), (0.00055, 0.0040760101), (0.1666, 0.0020067799), (0.00545, 0.021263899)]
Ajax1234
  • 69,937
  • 8
  • 61
  • 102