如何连接两个区间?

问题描述 投票:0回答:1
wells = [
    [
        [0, 0.4, 'pop'],
        [0.4, 1.2, 'sup'],
        [1.2, 2.4, 'gal'],
        [2.4, 3.2, 'grav40'],
        [3.2, 4, 'buz']
    ],
    [
        [0, 0.4, 'pop'],
        [0.4, 0.8, 'sup'],
        [0.8, 2.4, 'gal'],
        [2.4, 3.6, 'grav30'],
        [3.6, 4.4, 'buz']
    ]
]

wells
列表中的列表具有以下格式 - [间隔从、间隔到、间隔描述]

我们需要将第一个和第二个列表之间的“间隔”连接起来。这可以使用相同的区间描述来完成。

然后我们会得到这样的结果:

0.4 connects to 0.4
1.2 connects to 0.8
2.4 connects to 2.4
4 connects to 4.4

但是由于以下元素的文本内容具有相同的描述('buz' = 'buz'),所以我们需要得到以下结果:

0.4 connects to 0.4
1.2 connects to 0.8
2.4 connects to 2.4
3.2 connects to 3.6
4 connects to 4.4

我尝试了这段代码:

wells = [
    [
        [0, 0.4, 'pop'],
        [0.4, 1.2, 'sup'],
        [1.2, 2.4, 'gal'],
        [2.4, 3.2, 'grav40'],
        [3.2, 4, 'buz']
    ],
    [
        [0, 0.4, 'pop'],
        [0.4, 0.8, 'sup'],
        [0.8, 2.4, 'gal'],
        [2.4, 3.6, 'grav30'],
        [3.6, 4.4, 'buz']
    ]
]

def find_matching_points_all(list1, list2):
    matching_points = []

    for point1 in list1:
        for point2 in list2:
            if point1[2] == point2[2]:
                matching_points.append((point1[1], point2[1]))

                break
            
        return matching_points

matching_points = find_matching_points_all(wells[0], wells[1])

for pair in matching_points:
    print(f"{pair[0]} connects with {pair[1]}")
print()

图中的连接:

python math intervals
1个回答
0
投票

我认为你需要重塑你的数据来简化问题,现在你有了区间,但是你可以将每个区间表示为两个数据点 - 层的顶部和层的底部,以这种形式表示的匹配数据将只是一个字典查找。


wells = [
    [
        [0, 0.4, 'pop'],
        [0.4, 1.2, 'sup'],
        [1.2, 2.4, 'gal'],
        [2.4, 3.2, 'grav40'],
        [3.2, 4, 'buz']
    ],
    [
        [0, 0.4, 'pop'],
        [0.4, 0.8, 'sup'],
        [0.8, 2.4, 'gal'],
        [2.4, 3.6, 'grav30'],
        [3.6, 4.4, 'buz']
    ]
]

KEY_TOP = 0
KEY_BOTTOM = 1

def reshape(well):
    d = dict()
    for top, bottom, descr in well:
        d[(descr, KEY_TOP)] = top
        d[(descr, KEY_BOTTOM)] = bottom
    return d

well1 = reshape(wells[0])
well2 = reshape(wells[1])
keys = set(well1.keys()).intersection(well2.keys())
matching_points = [(well1[key], well2[key]) for key in keys]
matching_points.sort(key=lambda item: item[0])

之后您需要做的就是对结果进行重复数据删除

matching_points

如果

descr
在孔内不是唯一的,则此方法将不起作用,在这种情况下,您需要使用更复杂的匹配,例如查找所有可能的候选者并选择最接近的一个。

© www.soinside.com 2019 - 2024. All rights reserved.