如何扩展代码实现第二个列表区间起始点与第一个列表对应区间关联
井地层区间关联逻辑扩展问题
数据结构说明
wells中的每个子列表代表一口井的地层区间,每个区间格式为:[interval from, interval to, interval description],即**[区间起始深度, 区间结束深度, 地层描述]**
现有代码
wells = [ [ [0, 0.4, 'pop'], [0.4, 0.8, 'sup'], [0.8, 1.2, 'muka'], [1.2, 2, 'sand'], [2, 2.4, 'buz'] ], [ [0, 0.4, 'pop'], [0.4, 1.6, 'sand'], [1.6, 2, 'graviy'], [2, 3.2, 'galka'], [3.2, 3.6, 'buz'] ] ] def find_matching_points_all(*lists): matching_points = [] for i in range(len(lists) - 1): list1 = lists[i] list2 = lists[i + 1] prev_point = None for point1 in list1: found_match = False for point2 in list2: if point1[2] == point2[2]: matching_points.append((point1[1], point2[1])) prev_point = point2 found_match = True break if not found_match and prev_point is not None: matching_points.append((point1[1], prev_point[1])) 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()
现有关联规则
现有代码实现了两口井地层区间的终点关联,规则如下:
- 若两个区间的地层描述相同,关联二者的区间终点(即
point1[1]对应point2[1]) - 若当前区间无匹配描述,则将第一口井的当前区间终点,关联到第二口井的前一个匹配区间的终点
扩展需求
需要新增关联逻辑:将第二口井中[1.6, 2, 'graviy']区间的起始深度(1.6),与第一口井的[1.2, 2, 'sand']区间建立关联(对应需求中红色连线的逻辑)
解决方案
修改后的代码
wells = [ [ [0, 0.4, 'pop'], [0.4, 0.8, 'sup'], [0.8, 1.2, 'muka'], [1.2, 2, 'sand'], [2, 2.4, 'buz'] ], [ [0, 0.4, 'pop'], [0.4, 1.6, 'sand'], [1.6, 2, 'graviy'], [2, 3.2, 'galka'], [3.2, 3.6, 'buz'] ] ] def find_matching_points_all(*lists): matching_points = [] for i in range(len(lists) - 1): list1 = lists[i] list2 = lists[i + 1] prev_point = None # 保留原有list1到list2的关联逻辑 for point1 in list1: found_match = False for point2 in list2: if point1[2] == point2[2]: matching_points.append((point1[1], point2[1])) prev_point = point2 found_match = True break if not found_match and prev_point is not None: matching_points.append((point1[1], prev_point[1])) # 新增特定区间的反向关联逻辑 # 匹配graviy区间起始点与sand区间 for point2 in list2: if point2[2] == 'graviy': for point1 in list1: if point1[2] == 'sand': # 将graviy区间起始点关联到sand区间起始点 matching_points.append((point1[0], point2[0])) break 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()
逻辑说明
- 保留原有关联逻辑,确保之前的规则继续生效
- 新增反向关联处理:遍历第二口井的区间,找到
'graviy'区间后,匹配第一口井的'sand'区间,将二者的起始深度建立关联,满足需求中的红色连线逻辑 - 若需要通用化双向匹配规则,可进一步扩展为:当任意一口井的区间起始点对应另一口井的特定区间时,自动创建关联对
内容的提问来源于stack exchange,提问作者Tommy Vercetti
相关产品推荐
相关产品推荐

