簡體   English   中英

Python 3如何使用表示重疊坐標的元組鍵壓縮字典

[英]python 3 how to compress a dictionary with tuple keys representing overlapping coordinates

我有一本字典,其中的鍵是表示y線坐標(y0,y1)的元組,其中y0 = y線的底部坐標,y1 = y線的頂部坐標

data = { (622, 633): 'text1', (604, 619) : 'text2', (596, 633) : 'text3', (577, 587) : 'text4', (551, 587) : 'text5'  }

我希望能夠“壓縮”字典,以便在元組鍵下將具有最大元組y0,y1范圍的元組關聯的所有項組合在一起。 所以上面的字典應該像這樣結束:

data = { (596, 633) : 'text1 text2 text3', (551, 587) : 'text4 text5' }

我是python的新手,在此方面scratch之以鼻,不勝感激朝着正確的方向點頭。 謝謝。

怎么樣呢?

# Returns True if point yt is in the range (y0, y1) inclusive
def _in(yt, y): return y[0] <= yt <= y[1]

# Returns True if either segment intersects (or is contained in the other), False otherwise
def intersects(s1, s2): return _in(s1[0], s2) or _in(s1[1], s2) or _in(s2[0], s1) or _in(s2[1], s1)

# Returns an (i,e) tuple for each element e of lst that "intersects" (see prev. function) list lst
#   i is index of element e in lst
def find_intersections(s, lst): return [(i,e) for (i,e) in enumerate(lst) if intersects(s,e)]

# Returns a 2-tuple containing the minimum and maximum of the input segments s1, s2
def merge(s1, s2): return (min(s1[0], s2[0]), max(s1[1], s2[1]))
# Note: The functions _in and merge assume that y0,y1 are sorted, ascending

data = { (622, 633): 'text1', (604, 619) : 'text2', (596, 633) : 'text3', (577, 587) : 'text4', (551, 587) : 'text5'  }
new_data = {}

lst = list(data.keys())
while len(lst):
    rng = lst.pop()

    keys = [rng,]
    while True:
        updated = False
        for (i,match) in find_intersections(rng, lst):
            updated = True
            keys.append(match)
            rng = merge(rng, match)
            lst.pop(i)
        if not updated: break

    new_data[rng] = ' '.join(sorted(data[k] for k in keys))

print(new_data)      # {(551, 587): 'text4 text5', (596, 633): 'text1 text2 text3'}

編輯:針對Python3的一些調整

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM