将1个字符串列表的列表转换为元组列表

时间:2018-04-04 00:58:57

标签: python string list tuples

我有一个字符串列表,如下所示:

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

但我希望它是一个看起来像这样的元组列表:

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

有谁知道怎么做?

1 个答案:

答案 0 :(得分:0)

您可以使用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]))

输出:

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