PEP 234: Iterators 
Author Message
 PEP 234: Iterators

[Tim, gives a filteriter class]

[Guido]

Quote:
> With the latest CVS of 2.2, you don't have to write the filteriter
> class -- the built-in function filter() can do this:

>     >>> for k in filter(lambda k: k & 1 == 0, d):
>     ...     print k
>     ...
>     4
>     2

Since I checked in the change to filter() that made this work, you *might*
guess that I knew that <wink>.  There's still a difference, and in some apps
a crucial one:  using filter() directly materializes the entire result list
before the loop gets going, but the filteriter class works "on demand".  IOW,
the builtin filter() function still produces a list, not an iterator; the
filteriter class produces an iterator.  This can be important if, e.g., the
dict is very large, or there's reason to believe the loop may exit early.  So
either space or time concerns may drive a decision to use the class instead.


Mon, 20 Oct 2003 07:33:17 GMT  
 PEP 234: Iterators

Quote:
> [Peter Caven]
> > ...
> > I would really like to be able to write a loop over only the keys,
> > values or items in a dict that pass some test (filter):

> > for KeyFilter(),value  in dict:
> >   ...do something with the values that have keys that pass the filter.

> > I know this idea is half-baked.  Is there some other way of elegantly
> > doing this without doing an explicit test inside the loop?

[Tim]

Quote:
> List comprehensions support iteration + filtering directly, but materialize
> the whole result in one gulp.  The following works fine in current CVS
> Python, expoiting the proposed new 2.2 iterator gimmicks:

[code omitted]

> d = {1:2, 2:3, 3:4, 4:5}

> for k in filteriter(lambda k: k & 1 == 0, d):
>     print "even key", k  # prints 2 and 4

> for v in filteriter(lambda k: k & 1, d.itervalues()):
>     print "odd value", v # prints 3 and 5

> evenkeys = filteriter(lambda k: k & 1 == 0, d)
> bigevenkeys  = filteriter(lambda k: k > 2, evenkeys)

> for whee in bigevenkeys:
>     print whee # prints 4

> I think it should be clear that an iterator can support any
> computation whatsoever, including filtering, transforming,
> stuttering, ..., whatever you can dream up.  Plus they can be
> chained together (as in bigevenkeys above).

With the latest CVS of 2.2, you don't have to write the filteriter
class -- the built-in function filter() can do this:

    >>> for k in filter(lambda k: k & 1 == 0, d):
    ... print k
    ...
    4
    2
    >>>

--Guido van Rossum (home page: http://www.python.org/~guido/)



Mon, 20 Oct 2003 08:25:05 GMT  
 PEP 234: Iterators

Quote:

>>     >>> for k in filter(lambda k: k & 1 == 0, d):
>>     ...     print k
>>     ...
>>     4
>>     2

>Since I checked in the change to filter() that made this work, you *might*
>guess that I knew that <wink>.  There's still a difference, and in some apps
>a crucial one:  using filter() directly materializes the entire result list
>before the loop gets going, but the filteriter class works "on demand".

Nice to see python becomes lazy!

Sincerely yours, Roman Suzi
--

_/ Thursday, May 03, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "A conclusion is simply the place where you got tired of thinking." _/



Mon, 20 Oct 2003 12:18:19 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. PEP 234: Iterators

2. PEP 234: Iterators

3. PEP 234: Iterators (fwd)

4. Discussion about PEP 234: iterators

5. PEP 234: Iterators (fwd)

6. PEP 234 little bug?

7. Jun for Java 234

8. API routine CryptEncrypt() returns error 234

9. PEP 276 Simple Iterator for ints (fwd)

10. PEP 276 Simple Iterator for ints (fwd)

11. PEP 276 Simple Iterator for ints

12. PEP 276 Simple Iterator for ints

 

 
Powered by phpBB® Forum Software