将字符串列表转换为元组

时间:2016-05-27 09:37:47

标签: python list python-2.7 floating-point tuples

我有这个字符串列表:

['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)', '(39.3199500000, -76.6222000000)', '(39.2533200000, -76.6263600000)', '(39.3068100000, -76.6549700000)', '(39.2937500000, -76.6233700000)', '(39.3146700000, -76.6425300000)', '(39.3073300000, -76.6015900000)', '(39.2451900000, -76.6336400000)', '(39.3283000000, -76.5893200000)', '(39.3215400000, -76.6736800000)', '(39.3010000000, -76.5977400000)', '(39.3122600000, -76.6194200000)', '(39.3161400000, -76.5663900000)', '(39.3573500000, -76.6005300000)', '(39.3311200000, -76.6315100000)', '(39.3311200000, -76.6315100000)', '(39.2832900000, -76.5996300000)', '(39.2868200000, -76.6063900000)', '(39.3031200000, -76.6461100000)']

我需要将此字符串转换为元组,以便输出为:

[(39.2947500000, -76.6565600000),(39.3423900000, -76.5698300000)......]

我尝试使用float方法,但它给出了这个错误:

ValueError:无法将字符串转换为float:(39.2947500000,-76.6565600000)

提前致谢

4 个答案:

答案 0 :(得分:6)

>>> L=['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)', '(39.3199500000, -76.6222000000)', '(39.2533200000, -76.6263600000)', '(39.3068100000, -76.6549700000)', '(39.2937500000, -76.6233700000)', '(39.3146700000, -76.6425300000)', '(39.3073300000, -76.6015900000)', '(39.2451900000, -76.6336400000)', '(39.3283000000, -76.5893200000)', '(39.3215400000, -76.6736800000)', '(39.3010000000, -76.5977400000)', '(39.3122600000, -76.6194200000)', '(39.3161400000, -76.5663900000)', '(39.3573500000, -76.6005300000)', '(39.3311200000, -76.6315100000)', '(39.3311200000, -76.6315100000)', '(39.2832900000, -76.5996300000)', '(39.2868200000, -76.6063900000)', '(39.3031200000, -76.6461100000)']
>>> import ast
>>> list(map(lambda x:ast.literal_eval(x), L))
[(39.29475, -76.65656), (39.34239, -76.56983), (39.31995, -76.6222), (39.25332, -76.62636), (39.30681, -76.65497), (39.29375, -76.62337), (39.31467, -76.64253), (39.30733, -76.60159), (39.24519, -76.63364), (39.3283, -76.58932), (39.32154, -76.67368), (39.301, -76.59774), (39.31226, -76.61942), (39.31614, -76.56639), (39.35735, -76.60053), (39.33112, -76.63151), (39.33112, -76.63151), (39.28329, -76.59963), (39.28682, -76.60639), (39.30312, -76.64611)]

对于python 2.x:map(lambda x:ast.literal_eval(x), L)

编辑:一些解释:

ast代表抽象语法树。 literal_eval()eval()安全得多。

引自官方文件:

  

ast.literal_eval(node_or_string)安全地评估表达式节点或   Unicode或Latin-1编码的字符串,包含Python文字或   容器展示。提供的字符串或节点可能只包含   以下Python文字结构:字符串,数字,元组,列表,   dicts,booleans和None。

     

这可以用于安全地评估包含Python的字符串   来自不受信任来源的值,无需解析值   自己。它无法评估任意复杂性   表达式,例如涉及运算符或索引。

答案 1 :(得分:2)

您可以使用eval

a = ['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)', '(39.3199500000, -76.6222000000)', '(39.2533200000, -76.6263600000)', '(39.3068100000, -76.6549700000)', '(39.2937500000, -76.6233700000)', '(39.3146700000, -76.6425300000)', '(39.3073300000, -76.6015900000)', '(39.2451900000, -76.6336400000)', '(39.3283000000, -76.5893200000)', '(39.3215400000, -76.6736800000)', '(39.3010000000, -76.5977400000)', '(39.3122600000, -76.6194200000)', '(39.3161400000, -76.5663900000)', '(39.3573500000, -76.6005300000)', '(39.3311200000, -76.6315100000)', '(39.3311200000, -76.6315100000)', '(39.2832900000, -76.5996300000)', '(39.2868200000, -76.6063900000)', '(39.3031200000, -76.6461100000)'] 
b = [ eval(x) for x in a ]

答案 2 :(得分:1)

my_list = ['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)']
print [(float(x.split(',')[0][1:]), float(x.split(',')[1][1:-1])) for x in my_list]

这个解决方案不使用eval(我不喜欢使用它),它遍历字符串,将它们拆分为',',对它们进行子序列化以删除'(' ,''')'然后将它们转换为浮动

答案 3 :(得分:0)

如果你想避免import,你会感到懊恼 这可能是避免列表理解的地方,有利于清晰*:

lst = ['(39.2947500000, -76.6565600000)', '(39.3423900000, -76.5698300000)', '(39.3199500000, -76.6222000000)', '(39.2533200000, -76.6263600000)', '(39.3068100000, -76.6549700000)', '(39.2937500000, -76.6233700000)', '(39.3146700000, -76.6425300000)', '(39.3073300000, -76.6015900000)', '(39.2451900000, -76.6336400000)', '(39.3283000000, -76.5893200000)', '(39.3215400000, -76.6736800000)', '(39.3010000000, -76.5977400000)', '(39.3122600000, -76.6194200000)', '(39.3161400000, -76.5663900000)', '(39.3573500000, -76.6005300000)', '(39.3311200000, -76.6315100000)', '(39.3311200000, -76.6315100000)', '(39.2832900000, -76.5996300000)', '(39.2868200000, -76.6063900000)', '(39.3031200000, -76.6461100000)']

out = list()
for i in lst:
    j,k = i.split(",")
    out.append(tuple((float(j[1:]), float(k[:-1]))))
print(out)

*见https://www.python.org/dev/peps/pep-0020/