[Solved] group given list elements using certain criteria


The only thing I could think of was to iterate over all polygons, and then loop over all the other polygons to find intersections. (Which means the algorithm has a quadratic run time complexity.) Inefficient, but it gets the job done.

result = []
for i, shape in enumerate(polys):
    intersected_shapes = [poly for poly in polys[i+1:] if shape.intersects(poly)]
    if intersected_shapes:
        intersected_shapes.append(shape)
        result.append(intersected_shapes)

With the input [a, b, c] from the question (this one), this produces the output:

[[b, a], [c, b]]

2

solved group given list elements using certain criteria