P: n/a

[a+b for a,b in zip(xrange(1,51), xrange(50,0,1))]
[51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51]
i want to add all the elemtns a s well. can i do this all in a
listcomprehension?
i can do this ofc:
reduce(lambda x,y:x+y,[a+b for a,b in zip(xrange(1,51),
xrange(50,0,1))])
but reduce is a functional way of doing it, what is the more pythonic
way of doing this?  
Share this Question
P: n/a

On Jun 22, 6:32*pm, cirfu <circularf...@yahoo.sewrote:
[a+b for a,b in zip(xrange(1,51), xrange(50,0,1))]
[51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51]
i want to add all the elemtns a s well. can i do this all in a
listcomprehension?
i can do this ofc:
reduce(lambda x,y:x+y,[a+b for a,b in zip(xrange(1,51),
xrange(50,0,1))])
but reduce is a functional way of doing it, what is the more pythonic
way of doing this?
Nothing that would be more concise than reduce. Anything you did with
iteration would just mimic reduce in any case.
Jeff Ober
artfulcode.net  
P: n/a

On Jun 23, 10:32*am, cirfu <circularf...@yahoo.sewrote:
[a+b for a,b in zip(xrange(1,51), xrange(50,0,1))]
[51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51]
i want to add all the elemtns a s well. can i do this all in a
listcomprehension?
i can do this ofc:
reduce(lambda x,y:x+y,[a+b for a,b in zip(xrange(1,51),
xrange(50,0,1))])
but reduce is a functional way of doing it, what is the more pythonic
way of doing this?
Use the builtin 'sum' function.
sum(a + b for a, b in zip(xrange(1, 51), xrange(50, 0, 1)))

Paul Hankin  
P: n/a

On Jun 23, 9:23 am, Paul Hankin <paul.han...@gmail.comwrote:
On Jun 23, 10:32 am, cirfu <circularf...@yahoo.sewrote:
[a+b for a,b in zip(xrange(1,51), xrange(50,0,1))]
[51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51,
51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51]
i want to add all the elemtns a s well. can i do this all in a
listcomprehension?
i can do this ofc:
reduce(lambda x,y:x+y,[a+b for a,b in zip(xrange(1,51),
xrange(50,0,1))])
but reduce is a functional way of doing it, what is the more pythonic
way of doing this?
Use the builtin 'sum' function.
sum(a + b for a, b in zip(xrange(1, 51), xrange(50, 0, 1)))
Instead of sum(a + b for a, b in zip(foo, bar))
why not use sum(foo) + sum(bar)
?  
P: n/a

John Machin wrote:
>
Instead of sum(a + b for a, b in zip(foo, bar))
why not use sum(foo) + sum(bar)
?
or even sum(foo+bar) as may apply.
Cheers, BB  
P: n/a

Le Monday 23 June 2008 11:39:44 Boris Borcic, vous avez écrit*:
John Machin wrote:
Instead of sum(a + b for a, b in zip(foo, bar))
why not use sum(foo) + sum(bar)
?
or even sum(foo+bar) as may apply.
Because some are better than others :
sum(foo+bar) is the worst, it create a superfluous list of len(foo) + len(bar)
elements.
sum(a + b for a, b in zip(foo, bar)), creates a list of max(len(foo),
len(bar)) elements, in most cases it is the same as the former.
This could have been corrected using itertools.izip.
So the winner is sum(foo) + sum(bar), which does not create anything not
needed.
But if the question is "how to build the list and sum up all elements in a
efficient way for sequences of arbitrary length ", it's important to make it
in the same iteration, so the most effective/clear, and so "pythonic", way to
do this is (untested) :
res, sum = [], 0
for s in (a + b for a, b
in zip(itertools.izip(xrange(1, 51),
xrange(50, 0, 1)))):
sum += s
res.append(sum)
Which is "pythonic" in first place is to provide sequences of datas as
iterators when they can grow in size.

_____________
Maric Michaud  
P: n/a

On Jun 23, 9:16 pm, Maric Michaud <ma...@aristote.infowrote:
Le Monday 23 June 2008 11:39:44 Boris Borcic, vous avez écrit :
John Machin wrote:
Instead of sum(a + b for a, b in zip(foo, bar))
why not use sum(foo) + sum(bar)
?
or even sum(foo+bar) as may apply.
Because some are better than others :
sum(foo+bar) is the worst, it create a superfluous list of len(foo) + len(bar)
elements.
sum(a + b for a, b in zip(foo, bar)), creates a list of max(len(foo),
len(bar)) elements, in most cases it is the same as the former.
This could have been corrected using itertools.izip.
So the winner is sum(foo) + sum(bar), which does not create anything not
needed.
But if the question is "how to build the list and sum up all elements in a
efficient way for sequences of arbitrary length ", it's important to make it
in the same iteration, so the most effective/clear, and so "pythonic", wayto
do this is (untested) :
res, sum = [], 0
Please use tot or total, not sum!
for s in (a + b for a, b
in zip(itertools.izip(xrange(1, 51),
Perhaps you should not have left zip() in there ...
xrange(50, 0, 1)))):
sum += s
res.append(sum)
Do you mean res.append(s) ?
I would have thought that it would have been better to create the list
and then sum it:
res = [a + b for a, b in itertools.izip(foo_iter, bar_iter)]
total = sum(res)
Cheers,
John  
P: n/a

Le Monday 23 June 2008 13:51:34 John Machin, vous avez écrit*:
On Jun 23, 9:16 pm, Maric Michaud <ma...@aristote.infowrote:
Le Monday 23 June 2008 11:39:44 Boris Borcic, vous avez écrit :
John Machin wrote:
Instead of sum(a + b for a, b in zip(foo, bar))
why not use sum(foo) + sum(bar)
?
>
or even sum(foo+bar) as may apply.
Because some are better than others :
sum(foo+bar) is the worst, it create a superfluous list of len(foo) +
len(bar) elements.
sum(a + b for a, b in zip(foo, bar)), creates a list of max(len(foo),
len(bar)) elements, in most cases it is the same as the former.
This could have been corrected using itertools.izip.
So the winner is sum(foo) + sum(bar), which does not create anything not
needed.
But if the question is "how to build the list and sum up all elements in
a efficient way for sequences of arbitrary length ", it's important to
make it in the same iteration, so the most effective/clear, and so
"pythonic", way to do this is (untested) :
res, sum = [], 0
Please use tot or total, not sum!
for s in (a + b for a, b
in zip(itertools.izip(xrange(1, 51),
Perhaps you should not have left zip() in there ...
xrange(50, 0,
1)))): sum += s
res.append(sum)
Do you mean res.append(s) ?
Yes, my mistakes, all remarks are indeed true. Sorry for this out of thoughts
sketch.
I would have thought that it would have been better to create the list
and then sum it:
res = [a + b for a, b in itertools.izip(foo_iter, bar_iter)]
total = sum(res)
This is good enough if you accept the overhead of iterating twice over the
whole list.
But this may be a bit more complicated, for efficiency, there is a better way
of allocating memory than successively appending new item.
I guess this should be a far better solution, but you'll need to know the
required number of iteration (the size of your iterators, the xrange in this
sample) :
res, total = [None] * n, 0
for i, s in enumerate(a + b for a, b
in izip(xrange(1, n+1),
xrange(n, 0, 1)):
total += s
res[i] =s

_____________
Maric Michaud   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 882
 replies: 7
 date asked: Jun 27 '08
