【发布时间】:2015-06-11 00:44:56
【问题描述】:
我正在尝试合并通过sys.argv 传入的数字对。
示例:
python myscript.py -35.12323 112.76767 -36.33345 112.76890 -33.68689 111.8980
我的目标是将这些变成元组中的二元组。 像这样:
((-35.12323,112.76767),(-36.33345,112.76890),(-33.68689,111.8980))
到目前为止,这是我尝试过的,但是当我将结果传递给 Shapely Point 和 Polygon 方法时会出现问题。
from shapely.geometry import Polygon
from shapely.geometry import Point
import sys
def args_to_tuple(list):
arglist = []
for coord in list:
arglist.append(float(coord))
i = 0
polylist = []
for xory in arglist:
tmp = (arglist[i],arglist[i+1])
polylist.append(tmp)
return tuple(polylist)
poly = Polygon(args_to_tuple(sys.argv[3:]))
# xy pair for point is first two sys args
point = Point(args_to_tuple(sys.argv[1:3]))
print point.within(poly) # should result in true, but doesn't
print poly.contains(point) # should result in true, but doesn't
这似乎在 itertools 中很常见,但我似乎在该模块中找不到任何可以让您成对抓取项目的东西。
【问题讨论】:
-
请编辑问题(特别是标题)以反映,您不想将列表元素组合到元组的列表/元组中,但想了解为什么
.within和.contains返回False用你的代码。也许你应该问一个新问题 -
Alik,我知道我对元组问题的论点出了点问题,而不是 shapely 库,它现在正在工作,因为它被传递了一个有效的元组元组。感谢您的帮助!
标签: python list tuples itertools