我正在尝试组合通过传入的数字对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 中很常见,但我似乎在该模块中找不到任何可以让您成对抓取项目的内容。
In [10]: args_str = '-35.12323 112.76767 -36.33345 112.76890 -33.68689 111.8980'
In [11]: args = map(float, args_str.split())
In [12]: args
Out[12]: [-35.12323, 112.76767, -36.33345, 112.7689, -33.68689, 111.898]
In [13]: zip(args[::2], args[1::2])
Out[13]: [(-35.12323, 112.76767), (-36.33345, 112.7689), (-33.68689, 111.898)]
zip
可以替换为itertools.izip https://docs.python.org/2/library/itertools.html#itertools.izip它产生一个迭代器而不是一个列表。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)