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()
wells
中的每个列表都是这种格式 - [间隔从,间隔到,间隔描述]。
我的代码连接第一个和第二个列表中的点。第一个条件是相同描述的区间。第二个条件是,如果描述不同,那么我们将其连接到之前的区间。
我需要扩展代码,以便间隔 [1.6, 2, 'graviy'] 从第一个列表开始。即,在区间 [1.2, 2, 'sand'] 中。也就是说,事实上,在第一个列表中,我们有一个点,第二个列表的间隔将从该点开始。
图中,红线标记了需要创建的连接。绿线是我的代码已经可以创建的线
我试图寻找逻辑,但找不到
您可以将两个井层列表转换为队列,以有效地将两个井不共享名称的层出队,直到遇到公共层,此时切换到另一个井继续出队。
from collections import deque
def well_connections(wells):
common = set.intersection(*({name for *_, name in well} for well in wells))
wells = list(map(deque, wells))
this = 0
while all(wells):
if all(well[0][2] in common for well in wells):
yield tuple(well[0][1] for well in wells)
if len(wells[this]) > 1 and wells[this][1][2] in common:
this = 1 - this
wells[this].popleft()
while wells[this] and wells[this][0][2] not in common:
yield tuple(well[0][1] for well in wells)
wells[this].popleft()
this = 1 - this
wells[this].popleft()
这样:
list(well_connections(wells))
返回:
[(0.4, 0.4), (0.8, 0.4), (1.2, 0.4), (2, 1.6), (2, 2), (2, 3.2), (2.4, 3.6)]