[英]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.