I have one list of data as follows:
from shapely.geometry import box
data = [box(1,2,3,4), box(5,6,7,8), box(1,2,3,4)]
codes = ['A','B','C']
The list 'data' has following elements:
A = box(1,2,3,4)
B = box(5,6,7,8)
C = box(1,2,3,4)
I have to check if an element intersect with any other elements. If intersects, they should put in one tuple; and if not intersect they should put in different tuple. The expected result is:
result = [(A,C), (B)]
How to do it?
I tried it as:
results = []
for p,c in zip(data,codes):
for x in data:
if p.intersects(x): ##.intersects return true if they overlap else false
results.append(c)
print results
Keep a dict of objects mapped to A,B and C, a set of matched objects and only add the single elements that have no matches after we get to a new letter if they are not in our matched set as all possible combinations will have been tested: