I have a list of objects, each of the same kind.
Each object has its own list of objects (usually just 5-10 items)
What I used to do was:
for o in main_object_list:
obj_list = o.get_this_object_list()
for i in obj_list:
if i in main_object_list:
//do something with i
While this approach works, when main_object_list has, say, 100.000 elements, it goes horribly slow.
My workaround has been this:
for o in main_object_list:
o.flag = True
for o in main_object_list:
obj_list = o.get_this_object_list()
for i in obj_list:
if i.flag:
//do something with i
It goes several orders of magnitude faster (from 22 minutes to as little as 17 secs) but I suspect there may be a different, and better, approach. Moreover, this example works just becaue each object has a flag property, and by the way it is not so elegant to use a flag that may well have been setted/unsetted in other functions (if this function is called in the body of a parent function which uses the same flag mechanisms, this would mess everything up, setting every objects flag)
Is there a more correct pythonesque way to quickly check if an object is in main_object_list?
setin Python 2.6 and up,sets.Setdown to Python 2.3.