在Python中从数组中删除字符串引号

时间:2014-01-25 14:49:38

标签: python arrays python-2.7 numpy coordinates

我正试图摆脱我的数组中的一些字符,所以我只剩下xy坐标,用逗号分隔如下:

[[316705.77017187304,790526.7469308273]
 [321731.20991025254,790958.3493565321]]

我已使用zip()创建tuple xy值的numpy(作为字符串列表中的对),然后我将其转换为使用[['316705.77017187304,' '790526.7469308273,'] ['321731.20991025254,' '790958.3493565321,']] 的数组。该阵列目前看起来像这样:

map()

我需要输出为数组。

我很沮丧如何摆脱单引号和第二个逗号。我已经读过{{1}}可以将字符串更改为数字但我无法使其工作。

提前致谢

1 个答案:

答案 0 :(得分:1)

使用31.2. ast — Abstract Syntax Trees¶

import ast
xll =  [['321731.20991025254,' '790958.3493565321,'], ['321731.20991025254,' '790958.3493565321,']]
>>> [ast.literal_eval(xl[0]) for xl in xll]
[(321731.20991025254, 790958.3493565321), (321731.20991025254, 790958.3493565321)]

上面给出了列表列表的元组列表,类型如下:

>>> [list(ast.literal_eval(xl[0])) for xl in xll]
[[321731.20991025254, 790958.3493565321], [321731.20991025254, 790958.3493565321]]
老人:我想这个:

>>> sll
[['316705.770172', '790526.746931'], ['321731.20991', '790958.349357']]
>>> fll = [[float(i) for i in l] for l in sll]
>>> fll
[[316705.770172, 790526.746931], [321731.20991, 790958.349357]]
>>> 

旧编辑:

>>> xll =  [['321731.20991025254,' '790958.3493565321,'], ['321731.20991025254,' '790958.3493565321,']]
>>> [[float(s) for s in xl[0].split(',') if s.strip() != ''] for xl in xll]
[[321731.20991025254, 790958.3493565321], [321731.20991025254, 790958.3493565321]]