[python-nl] Venster over een lijst schuiven

Dirkjan Ochtman dirkjan at ochtman.nl
Sun Oct 17 17:19:48 CEST 2010


2010/10/17 Rob Hooft <rob at hooft.net>:
> En zo?
>
> class Window(object):
>        def __init__(self, n):
>                self.n = n
>
>        def __call__(self, list):
>                ret = []
>                for i in list:
>                        if len(ret) == self.n:
>                                yield ret
>                                ret = ret[1:]
>                        ret.append(i)
>                if len(ret) == self.n:
>                        yield ret
>
> lijst = [1, 2, 3, 3, 4, 10 , 3 ,2, 2, 1]
>
> w=Window(3)
> for three in w(lijst):
>        print three

Waarom gebruik je hier een class? Je kan die hele __call__() gewoon
als functie schrijven.

Cheers,

Dirkjan


More information about the Python-nl mailing list