fast way to filter a set?
franke at ableton.com
Wed Sep 17 19:25:25 CEST 2003
from itertools import ifilter
> But this created two lists (keys and dummy)? Compared to the filter()
> method, which created a list and a new set, maybe your suggestion is
> still a bit faster...
> --- Skip Montanaro <skip at pobox.com> wrote:
>> fortepianissimo> I know I can do things like
>> fortepianissimo> s=Set(range(1,11))
>> fortepianissimo> s=Set(filter(lambda x:x%2==0,s))
>> fortepianissimo> But this seems a bit slow since filter returns a
>> fortepianissimo> which then must be converted back to a set. Any
>>The only thing which comes to mind is:
>> >>> s = sets.Set(range(1,11))
>> >>> s
>> Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>> >>> keys = list(s)
>> >>> dummy = [s.discard(x) for x in keys if x%2]
>> >>> s
>> Set([2, 4, 6, 8, 10])
>>You still create a list, but don't create a second set.
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site design software
More information about the Python-list