繁体   English   中英

每秒出现未知元素后分割字符串

[英]Split string after every second occurence of unknown element

我有一个带有代表多边形的坐标列表的字符串。 在此列表中,每个面都有相同的开始和结束坐标。 我需要在单独的字符串(或列表)中包含每个多边形。

'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'

因此,从这个简单的示例中,我需要包含两个元素:

一个= '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 '*

字符串中可以有更多的多边形,每个多边形需要分开。 我只能为此使用标准的python库

这是一个相当丑陋但可行的解决方案,只是将明显的方法真正地应用到了代码中。

# 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']
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再次是第一对坐标。

>>> 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)]]

对于更长的字符串,这可能不是最快的解决方案,但是我没有计时。

如何用每个“,”拆分长字符串,然后将其放入数组。 然后进行for循环并执行:

intStart = 0;
if (array[intStart] == array[i]){
    for(j=0; j<i; j++){
        string += array[j];
    }
    arrPolygons.push(string);
    intStart = i+1;
}

因此,当起始坐标与一个值匹配时,请将这些值之间的所有变量添加到字符串中。 然后将字符串推到另一个数组。 然后开始比较下一个值与之后的数据。

顺便说一下,这只是一个伪代码示例。

希望这可以帮助

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']]

输入数据...

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',
]

我真的很喜欢@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))

递归方法:

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']
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

这是另一种方法,该方法适用于任何字符串长度,只要它基于您提供的输入格式即可。

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])
>>> 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']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM