将字符串转换为2元组列表(Converting a string to a list of 2

系统教程 行业动态 更新时间:2024-06-14 16:53:13
将字符串转换为2元组列表(Converting a string to a list of 2-tuples)

我有这种形状的字符串:

d="M 997.14282,452.3622 877.54125,539.83678 757.38907,453.12006 802.7325,312.0516 950.90847,311.58322 Z"

它们是五边形的(x, y)坐标(第一个和最后一个字母是元数据并且被忽略)。 我想要的是一个2元组列表,它代表浮点中的坐标而不是所有的东西:

d = [(997.14282, 452.3622), (877.54125, 539.83678), (757.38907, 453.12006), (802.7325,312.0516), (950.90847, 311.58322)]

修剪字符串很简单:

>>> d.split()[1:-2] ['997.14282,452.3622', '877.54125,539.83678', '757.38907,453.12006', '802.7325,312.0516']

但现在我想以简洁的方式创建元组。 这显然不起作用:

>>> tuple('997.14282,452.3622') ('9', '9', '7', '.', '1', '4', '2', '8', '2', ',', '4', '5', '2', '.', '3', '6', '2', '2')

拿原始字符串,我可以这样写:

def coordinates(d): list_of_coordinates = [] d = d.split()[1:-2] for elem in d: l = elem.split(',') list_of_coordinates.append((float(l[0]), float(l[1]))) return list_of_coordinates

哪个工作正常:

>>> coordinates("M 997.14282,452.3622 877.54125,539.83678 757.38907,453.12006 802.7325,312.0516 950.90847,311.58322 Z") [(997.14282, 452.3622), (877.54125, 539.83678), (757.38907, 453.12006), (802.7325, 312.0516)]

然而,这个处理是一个更大的程序的一个小而微不足道的部分,我宁愿尽可能短而简洁。 任何人都可以告诉我一个不那么冗长的方法将字符串转换为2元组列表?

I have strings of this shape:

d="M 997.14282,452.3622 877.54125,539.83678 757.38907,453.12006 802.7325,312.0516 950.90847,311.58322 Z"

which are (x, y) coordinates of a pentagon (the first and last letters are metadata and to be ignored). What I want is a list of 2-tuples that would represent the coordinates in floating points without all the cruft:

d = [(997.14282, 452.3622), (877.54125, 539.83678), (757.38907, 453.12006), (802.7325,312.0516), (950.90847, 311.58322)]

Trimming the string was easy:

>>> d.split()[1:-2] ['997.14282,452.3622', '877.54125,539.83678', '757.38907,453.12006', '802.7325,312.0516']

but now I want to create the tuples in a succinct way. This obviously didn't work:

>>> tuple('997.14282,452.3622') ('9', '9', '7', '.', '1', '4', '2', '8', '2', ',', '4', '5', '2', '.', '3', '6', '2', '2')

Taking the original string, I could write something like this:

def coordinates(d): list_of_coordinates = [] d = d.split()[1:-2] for elem in d: l = elem.split(',') list_of_coordinates.append((float(l[0]), float(l[1]))) return list_of_coordinates

which works fine:

>>> coordinates("M 997.14282,452.3622 877.54125,539.83678 757.38907,453.12006 802.7325,312.0516 950.90847,311.58322 Z") [(997.14282, 452.3622), (877.54125, 539.83678), (757.38907, 453.12006), (802.7325, 312.0516)]

However this processing is a small and trivial part of a bigger program and I'd rather keep it as short and succinct as possible. Can anyone please show me a less verbose way to convert the string to the list of 2-tuples?

最满意答案

注意,不确定这是否是预期的 - 当你执行d.split()[1:-2] ,你正在丢失最后一个坐标。 假设这不是故意的,那么一个班轮就是 -

def coordinates1(d): return [tuple(map(float,coords.split(','))) for coords in d.split()[1:-1]]

如果故意丢失最后一个坐标,请在上面的代码中使用[1:-2] 。

A note, not sure if this is intended - when you do d.split()[1:-2] , you are losing the last coordinate. Assuming that is not intentional , A one liner for this would be -

def coordinates1(d): return [tuple(map(float,coords.split(','))) for coords in d.split()[1:-1]]

If losing the last coordinate is intentional, use [1:-2] in the above code.

更多推荐

本文发布于:2023-04-06 01:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/14993ec0467334b4628eb7e4f4cc98fe.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   字符串   列表   list   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!