Velocity Reviews > Re: calculation on lists

# Re: calculation on lists

Chris Kaynor
Guest
Posts: n/a

 12-19-2012
On Wed, Dec 19, 2012 at 4:38 AM, Vlastimil Brom <(E-Mail Removed)>wrote:

> 2012/12/19 loïc Lauréote <(E-Mail Removed)>:
> 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.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Vlastimil Brom Python 0 12-19-2012 05:24 PM loïc Lauréote Python 0 12-19-2012 03:24 PM Vlastimil Brom Python 0 12-19-2012 12:38 PM loïc Lauréote Python 0 12-19-2012 12:25 PM =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM