calculation on lists

Chris Kaynor ckaynor at zindagigames.com
Wed Dec 19 12:11:14 EST 2012


On Wed, Dec 19, 2012 at 4:38 AM, Vlastimil Brom <vlastimil.brom at gmail.com>wrote:

> 2012/12/19 loïc Lauréote <laureote-loic at hotmail.fr>:
> hi,
> I
>  have a question,
> is there a tool to calculate on list ?
>
> something like :
>
> >a= [1,1,1,1]
> >b = [5,9,8,4]
> >c = a+b*a
> >print c
> >[6,10,9,5]
>
> Thx
>
> ======
>
> Hi,
> for such simpler cases, you may try list comprehensions and probably
> the zip(...) function
>
> >>> [a+b*a for a,b in zip([1,1,1,1], [5,9,8,4])]
> [6, 10, 9, 5]
> >>>
>

You can also use map (Python 2.6):
map(lambda a,b: a+b*a, [1,1,1,1], [5,9,8,4])

Note that the lambda can be replaced by any callable which takes 2
arguments.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121219/1c40ab63/attachment.html>


More information about the Python-list mailing list