Question: I have a list of wells that stores two wells and the contents of these wells in the form [depth interval from, depth interval to, soil description] wells = [ [[0, -4, 'soil'], [-4, -8, 'clay'], [-8, -12, ' gravel'], [-12, -20, 'sand'], [-20, -24, 'basalts']], [[0, -4, 'soil'], [-4, -16, 'sand'], [-16, -20, 'galka'], [-20, -32, 'pebble'], [-32, -36, 'basalts']] ]  We need to draw lines between all the soil layers so that we can get a full geological section with all the layers This is what these two wells look like, where different or identical rocks are marked in color. you need to connect all the same rocks and correctly identify those rocks that are only in one well  and this is what the final result of all connections looks like:  all these connections can be made based on the data that I provided. import matplotlib.pyplot as plt wells = [ [[0, -4, 'soil'], [-4, -8, 'clay'], [-8, -12, ' gravel'], [-12, -20, 'sand'], [-20, -24, 'basalts']], [[0, -4, 'soil'], [-4, -16, 'sand'], [-16, -20, 'galka'], [-20, -32, 'pebble'], [-32, -36, 'basalts']] ] fig, ax = plt.subplots() depths_dict = {} colors = {'soil': 'red', 'sand': 'blue', 'gravel': 'green', 'pebble': 'orange', 'basalts': 'purple', 'clay': 'brown', 'galka': 'pink'} for well_index, well in enumerate(wells): for interval in well: start_depth, end_depth, description = interval color = colors.get(description, 'gray') if description in depths_dict: ax.plot([well_index - 1, well_index], [depths_dict[description][0], start_depth], color=color, linestyle='--') ax.plot([well_index - 1, well_index], [depths_dict[description][1], end_depth], color=color, linestyle='--') depths_dict[description] = (start_depth, end_depth) ax.plot([well_index, well_index], [start_depth, end_depth], color=color, label=description) ax.set_xlabel('Wells') ax.set_ylabel('Depth') ax.set_title('Profile') legend_handles = [plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=colors[rock], markersize=10, label=rock) for rock in colors] ax.legend(handles=legend_handles) plt.show()  but this code connects intervals with the same description

Answer: All i had to do was compare all the layers from top to bottom and draw connections so that the deeper layers lay lower than the shallower ones wells = [ [ [24, 20, 'basalts'], [20, 12, 'sand'], [12, 8, 'graviy'], [8, 4, 'clay'], [4, 0, 'soil'] ], [ [36, 32, 'basalts'], [32, 20, 'galka'], [20, 16, 'sheben'], [16, 4, 'sand'], [4, 0, 'soil'] ] ] def lith_layers_comparison(well_1, well_2): matching_points = [] i, j = 0, 0 while i < len(well_1) and j < len(well_2): p1, p2 = well_1[i], well_2[j] if p1[2] == p2[2]: matching_points.extend([[p1[0], p2[0]], [p1[1], p2[1]]]) i, j = i + 1, j + 1 elif p1[2] != p2[2] and p1[1] < p2[1]: matching_points.append([p1[0], p2[1]]) j += 1 elif p1[2] != p2[2] and p1[1] > p2[1]: matching_points.append([p1[1], p2[0]]) i += 1 elif p1[2] != p2[2] and p1[1] == p2[1] and p1[0] < p2[0]: matching_points.append([p1[0], p2[1]]) j += 1 elif p1[2] != p2[2] and p1[1] == p2[1] and p1[0] > p2[0]: matching_points.append([p1[1], p2[0]]) i += 1 elif p1[2] != p2[2] and p1[1] == p2[1] and p1[0] == p2[0]: if p1[3] > p2[3]: matching_points.append([p1[0], p2[1]]) j += 1 else: matching_points.append([p1[1], p2[0]]) i += 1 return matching_points reversed_connections = [] for i in range(len(wells) - 1): connections = lith_layers_comparison(wells[i], wells[i+1]) filtered_connections = [] for connection in connections: if connection not in filtered_connections: filtered_connections.append(connection) reversed_connections.append(filtered_connections) sorted_connections = [[list(map(lambda x: -x, sub)) for sub in sublist[::-1]] for sublist in reversed_connections] print(sorted_connections)