Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersect 2 lists, store result in a tuple in python

I thought what I had was a common problem, but I could not find any help either in Google nor in SO.

I have 2 lists that contain objects of class Marker. A Marker consists of variables name, position and type. I want to intersect the two lists, create tuples of markers of the same type and store them in a new list. Literally speaking, I want to do something like the following:

g_markerList = [ (marker1,marker2) for marker1 in marker1List and marker2 in marker2List if marker1.type == marker2.type ]

Apparently, this code does not work. Compiler does not ''know'' variable marker2 following and, which ends the for clause.

Please help me to intersect these two lists and obtain a list of tuples of similar markers!

like image 248
Pavlo Dyban Avatar asked Feb 21 '23 06:02

Pavlo Dyban


1 Answers

Change the and to for:

g_markerList = [ (marker1,marker2) for marker1 in marker1List
                                   for marker2 in marker2List
                                   if marker1.type == marker2.type ]
like image 98
Chris Pfohl Avatar answered Feb 23 '23 19:02

Chris Pfohl