fast way to filter a set?
Mike C. Fletcher
mcfletch at rogers.com
Wed Sep 17 13:03:58 EDT 2003
fortepianissimo wrote:
>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...
>
>
Generators will probably be the easiest way to avoid the list-creation
overhead...
>>> from __future__ import generators
>>> def fget( s, function ):
... for item in s:
... if function(item):
... yield item
...
>>> r = Set.Set( range(20))
>>> Set.Set( fget(r, lambda x: x%2))
[19, 1, 3, 17, 5, 7, 9, 11, 13, 15]
>>>
HTH,
Mike
>--- 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
>>list
>> fortepianissimo> which then must be converted back to a set. Any
>>tips?
>>
>>
...
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list