I have one list of data as follows:
from shapely.geometry import box
data = [box(1,2,3,4), box(4,5,6,7), box(1,2,3,4)]
sublists = [A,B,C]
The list 'data' has following sub-lists:
A = box(1,2,3,4)
B = box(4,5,6,7)
C = box(1,2,3,4)
I have to check if sub-lists intersect. If intersect they should put in one tuple; and if not intersect 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,sub_lists):
for x in data:
if p.intersects(x): ##.intersects return true if they overlap else false
results.append(c)
print results
Without downloading
shapely, I think what you want to do with lists can be replicated with strings (or integers):Replace the
i in datawith yourintersectstests. The first sublist ofresultscontains the elements ofdata1for which the test is true. The second sublist contains the elements where it is false.Your question is a little confusing in that
dataandsublistsappear to contain the same elements. So maybe you aren't testing whetherAis indata(or intersect with an element ofdata), but whetherAintersects with an other element of[A,B,C], etc.In any case, the key to collecting the results is to have two (or more) slots in
results, where you can putidepending on the test.resultscould also be a dictionary, or could be two different variables. e.g.results={'found':[],'lost':[]}.Do we need to work more on the test?