我有一个带有表示多边形的坐标列表的字符串。 在此列表中,每个面都有相同的开始和结束坐标。 我需要将每个多边形放在单独的字符串(或列表)中。
' 17.17165756225586 -28.102264404296875 ,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672, 17.17165756225586 -28.102264404296875 , 28.865726470947266 -28.761619567871871 ,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094 '
因此,从这个简单的示例中,我需要具有两个元素:
一个=' 17.17165756225586 -28.102264404296875 ,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672, 17.17165756225586 -28.102264404296875 '
Two =' 28.865726470947266 -28.761619567871094 ,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094 '*
字符串中可以有更多的多边形,每个多边形需要分开。 我只能为此使用标准的python库
答案 0 :(得分:2)
这是一个非常丑陋但可行的解决方案,只是将明显的方法真正地应用到了代码中。
# Note that your string has inconsistent separators -- sometimes ',', sometimes ', '.
# I'm going to separate on `,` and not worry about it -- you need to work out
# what the correct separator is.
s = '17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
coordinates = s.split(',')
polygon = []
polygons = []
new = True
for coordinate in coordinates:
polygon.append(coordinate)
if new:
start = coordinate
new = False
elif coordinate == start:
polygons.append(polygon)
polygon = []
new = True
result = [",".join(polygon) for polygon in polygons]
print(result)
Out:
['17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875', ' 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094']
答案 1 :(得分:2)
s='17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
#convert the input in a list of points
coordinates = [tuple(map(float,el.split())) for el in s.split(",")]
polygons = []
#find the polygons
while coordinates:
ind = coordinates[1:].index(coordinates[0])
polygons.append(coordinates[0:ind+2])
coordinates = coordinates[ind+2:]
#output
[(17.17165756225586, -28.102264404296875), (17.184370040893555, -28.200496673583984), (17.1986083984375, -28.223613739013672), (17.17165756225586, -28.102264404296875)]
[(28.865726470947266, -28.761619567871094), (28.80694007873535, -28.75750160217285), (28.792499542236328, -28.706947326660156), (28.865726470947266, -28.761619567871094)]
答案 2 :(得分:2)
由于您的输入已经是字符串(并且您还期望得到结果吗?),因此您可以使用带有反向引用的regular expression (([^,]+).*\2)
来尝试这种超级懒惰的解决方案。这里,[^,]+
是第一对坐标,.*
是其他坐标对,\2
也是第一对坐标。
>>> s = '17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
>>> re.findall(r"(([^,]+).*\2)", s)
[('17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875',
'17.17165756225586 -28.102264404296875'),
(' 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094',
' 28.865726470947266 -28.761619567871094')]
或使用finditer
并获取group
直接获取字符串列表:
>>> [m.group() for m in re.finditer(r"(([^,]+).*\2)", s)]
['17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875',
' 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094']
经过一些后处理,以获取数字对的实际列表(_
是findall
的结果;对于finditer
,请删除[0]
):
>>> [[tuple(map(float, y.split())) for y in x[0].split(",")] for x in _]
[[(17.17165756225586, -28.102264404296875),
(17.184370040893555, -28.200496673583984),
(17.1986083984375, -28.223613739013672),
(17.17165756225586, -28.102264404296875)],
[(28.865726470947266, -28.761619567871094),
(28.80694007873535, -28.75750160217285),
(28.792499542236328, -28.706947326660156),
(28.865726470947266, -28.761619567871094)]]
对于更长的字符串,这可能不是最快的解决方案,但是我没有计时。
答案 3 :(得分:0)
如何用每个“,”分割长字符串并将其放入数组。然后创建一个for循环并执行:
intStart = 0;
if (array[intStart] == array[i]){
for(j=0; j<i; j++){
string += array[j];
}
arrPolygons.push(string);
intStart = i+1;
}
因此,当起始坐标与一个值匹配时,请将这些值之间的所有变量添加到字符串中。然后将字符串推到另一个数组。然后开始比较下一个值和之后的数据。
这只是一个伪代码示例。
希望这会有所帮助
答案 4 :(得分:0)
s = '17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
coord = s.split(',')
inpoly = False
poly = [[]]
start = None
for i in coord:
poly[-1].append(i)
if i == start:
poly.append([])
inpoly = False
if not inpoly:
start = i
inpoly = True
print(poly)
输出:
[['17.17165756225586 -28.102264404296875', '17.184370040893555 -28.200496673583984', '17.1986083984375 -28.223613739013672', '17.17165756225586 -28.102264404296875'], [' 28.865726470947266 -28.761619567871094', '28.80694007873535 -28.75750160217285', '28.792499542236328 -28.706947326660156', ' 28.865726470947266 -28.761619567871094']]
答案 5 :(得分:0)
输入数据...
lst = [
'17.17165756225586 -28.102264404296875',
'17.184370040893555 -28.200496673583984',
...
'17.1986083984375 -28.223613739013672',
'17.17165756225586 -28.102264404296875',
'28.865726470947266 -28.761619567871094',
...
'28.80694007873535 -28.75750160217285',
'28.792499542236328 -28.706947326660156',
'28.865726470947266 -28.761619567871094',
]
lst1 = []
for cord in lst:
if cord not in lst1:
lst1.append(cord)
print(lst1)
输出:
[
'17.17165756225586 -28.102264404296875',
'17.184370040893555 -28.200496673583984',
'17.1986083984375 -28.223613739013672',
'28.865726470947266 -28.761619567871094',
'28.80694007873535 -28.75750160217285',
'28.792499542236328 -28.706947326660156',
'28.865726470947266 -28.761619567871094',
]
答案 6 :(得分:0)
我真的很喜欢@newbie的解决方案。这是更详细/可读的一个:
s = '17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
vertices = [c.strip() for c in s.split(",")] # split and clean vertex data
polygons = []
current_polygon = None
for vertex in vertices:
if current_polygon is None: # start a new polygon
current_polygon = [vertex]
elif current_polygon[0] == vertex: # conclude the current polygon
current_polygon.append(vertex)
polygons.append(current_polygon)
current_polygon = None
else: # continue the current polygon
current_polygon.append(vertex)
for polygon in polygons: # print polygons
print(",".join(polygon))
答案 7 :(得分:0)
一种递归方法:
def split_polygons(s):
if s == '': # base case
return []
start, rest = s.split(',', 1)
head, tail = map(lambda x: x.strip(', '), rest.split(start, 1))
poly = start + ',' + head + start # reconstruct the first polygon
return [poly] + split_polygons(tail)
>>> p = '17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875, 28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
>>> split_polygons(p)
['17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,17.1986083984375 -28.22361373901367217.17165756225586 -28.102264404296875', '28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,28.792499542236328 -28.70694732666015628.865726470947266 -28.761619567871094']
答案 8 :(得分:0)
c = '17.17165756225586 -28.102264404296875,17.184370040893555 -28.200496673583984,\
17.1986083984375 -28.223613739013672,17.17165756225586 -28.102264404296875,\
28.865726470947266 -28.761619567871094,28.80694007873535 -28.75750160217285,\
28.792499542236328 -28.706947326660156, 28.865726470947266 -28.761619567871094'
c = [i.strip(' ') for i in c.split(',')]
i = 0
lst = []
while i!=len(c):
out = c[i]
j = i+1
while c[i]!=c[j]:
out = out+','+c[j]
j = j+1
out = out+','+c[j]
lst.append(out)
out=''
i = j+1
答案 9 :(得分:0)
这是另一种方法,该方法适用于任何字符串长度,只要它取决于您提供的输入格式即可。
strng = "17.17165756225586,-28.102264404296875,17.184370040893555,-28.200496673583984,17.1986083984375,-28.223613739013672,17.17165756225586,-28.102264404296875,28.865726470947266,-28.761619567871094,28.80694007873535,-28.75750160217285,28.792499542236328,-28.706947326660156,28.865726470947266,-28.761619567871094"
#convert to list of tuples
l_tuple = zip(*[iter(strng.split(','))]*2)
#get list of duplicate indexes
l_index=[]
for Tuple in l_tuple:
x = [i for i,x in enumerate(l_tuple) if x == Tuple]
if len(x)>1:
l_index.append(x)
#get separate lists
New_list = []
for IND in list(set(map(tuple,l_index))):
print(l_tuple[IND[0]:IND[1]+1])
New_list.append(l_tuple[IND[0]:IND[1]+1])
答案 10 :(得分:-1)
>>> lst = ['17.17165756225586 -28.102264404296875','17.184370040893555 -28.200496673583984',\
... '17.1986083984375 -28.223613739013672','17.17165756225586 -28.102264404296875',' 28.865726470947266 -28.761619567871094',\
... '28.80694007873535 -28.75750160217285','28.792499542236328 -28.706947326660156', '28.865726470947266 -28.761619567871094']
>>> lst1 =[]
>>> for cord in lst:
... if cord not in lst1:
... lst1.append(cord)
...
>>> print(lst1)
['17.17165756225586 -28.102264404296875', '17.184370040893555 -28.200496673583984', '17.1986083984375 -28.223613739013672', ' 28.865726470947266 -28.761619567871094', '28.80694007873535 -28.75750160217285', '28.792499542236328 -28.706947326660156', '28.865726470947266 -28.761619567871094']