[Zope] Re: Intersection/Union of ZCatalog result sets
Max M
maxm at mxm.dk
Sat Sep 25 10:48:19 EDT 2004
Jonathan Hobbs wrote:
>>should run them through a "unique" routine -- there was a good one in the
>>CMFCalendar.CalendarTool.py routines, which I extracted and put into a
>>Python script in CalendarX.
>>
>>Here's the code, in its entirety:
>># Unique the results of a query (query)
>>results = []
>>rids = []
>>for item in query:
>> rid = item.getRID()
>> if not rid in rids:
>> results.append(item)
>> rids.append(rid)
That is about the worst way to implement a unique routine. As "rids"
grow, you get a O^2 time.
Using a dict is much faster, at least for larger result sets.
unique = {}
for item in query:
unique[item.getRID()] = item
return unique.values()
You could probably even do it as:
unique = {}
for item in query:
unique[item] = None
return unique.keys()
But that depends on whether the objects are hashable.
--
hilsen/regards Max M, Denmark
http://www.mxm.dk/
IT's Mad Science
More information about the Zope
mailing list