Python style.

Kevin Russell krussell4 at videon.home.com
Wed May 10 17:54:26 EDT 2000


Kevin Russell wrote:

> Fredrik Lundh wrote:
>
> > Jacek Generowicz <jmg at ecs.soton.ac.uk> wrote:
> > > How would you rewrite the following code, in good
> > > python style ?
> > >
> > > list1 = [ 1,2,3,4,5,6 ]
> > > list2 = [ 6,5,4,3,2,1 ]
> > >
> > > count = 0
> > > for item in list1:
> > >     print item - list2[count]
> > >     count = count + 1
> >
> >
> > the slightly more obscure way:
> >
> >     for item1, item2 in map(None, list1, list2):
> >         ...
> >
>
> If you're going functional, you might as well go all the way:
>
>      reduce(operator.add, map(operator.sub, l1, l2))
>
> Still-speaking-Python-with-a-Scheme-accent-ly yours,
> Kevin

Oops, Jacek wanted "count" to accumulate a simple count,
not the sum of the differences.  That's what I get for trying to
read in the morning.  Bad morning! Go sit in your corner!

-- Kevin





More information about the Python-list mailing list