在Python中从数组中删除字符串引号 [英] Remove string quotes from array in Python

查看:1791
本文介绍了在Python中从数组中删除字符串引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get rid of some characters in my array so I'm just left with the x and y coordinates, separated by a comma as follows:

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

我已经使用zip()来创建xy值的一个tuple(作为字符串列表中的对),然后使用numpy将其转换为数组.该数组当前如下所示:

I have used zip() to create a tuple of the x and y values (as pairs from a list of strings), which I've then converted to an array using numpy. The array currently looks like this:

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

我需要输出为数组.

关于如何摆脱单引号和第二个逗号,我感到很困惑.我已经读过map()可以将字符串更改为数字,但是我无法使其正常工作.

I'm pretty stumped about how to get rid of the single quotes and the second comma. I have read that map() can change string to numeric but I can't get it to work.

预先感谢

推荐答案

使用 31.2. ast —抽象语法树¶

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)]

上面给出了元组列表作为列表列表,键入以下内容:

Above gives list of tuples for list of list, type following:

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

OLD:我认为:

>>> 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]]

这篇关于在Python中从数组中删除字符串引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆