我有这种形状的字符串:
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_coordinateswhich 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.
更多推荐
发布评论