python - 将字符串列表转换为元组

标签 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)

提前致谢

最佳答案

>>> 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) Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

关于python - 将字符串列表转换为元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37480031/

相关文章:

python - 谷歌应用引擎 NDB

Python - 升级我的迷宫求解器程序

php - 在什么情况下,如果我们将排序后的数字作为键添加到哈希表中,我们可以期望哈希是有序的?

sqlite - sqlalchemy - (ProgrammingError) 可以解释 8 位字节串

python - 将 Django 项目推送到 Heroku 时出错

python - groupby 和过滤 Pandas

python - 排序双向链表 Python

java - 列表值被新值替换

Java 数组列表 : Difference between Copy Constructor and Copying via assignment

python - 当我尝试将 excel 文件转换为列表时,“DataFrame”对象没有属性 'tolist'