Simple Problem but tough for me if i want it in linear time

MRAB python at mrabarnett.plus.com
Sun Aug 15 09:29:15 EDT 2010


ChrisChia wrote:
> dataList = [a, b, c, ...]
> where a, b, c are objects of a Class X.
> In Class X, it contains self.name and self.number
> 
> If i wish to test whether a number (let's say 100) appears in one of
> the object, and return that object,
> is that only fast way of solving this problem without iterating
> through every object to see the number value?
> 
> dataList.__contains__ can only check my object instance name...
> anyone can solve this in linear complexity?

Put them into a dict where the key is the number and the value is a list
of the objects with that number.



More information about the Python-list mailing list