473,573 Members | 3,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

listcomprehensi on, add elements?

[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
listcomprehensi on?

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?
Jun 27 '08 #1
7 1007
On Jun 22, 6:32*pm, cirfu <circularf...@y ahoo.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
listcomprehensi on?

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
Jun 27 '08 #2
On Jun 23, 10:32*am, cirfu <circularf...@y ahoo.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
listcomprehensi on?

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
Jun 27 '08 #3
On Jun 23, 9:23 am, Paul Hankin <paul.han...@gm ail.comwrote:
On Jun 23, 10:32 am, cirfu <circularf...@y ahoo.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
listcomprehensi on?
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)
?
Jun 27 '08 #4
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

Jun 27 '08 #5
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.i zip(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
Jun 27 '08 #6
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.i zip(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
Jun 27 '08 #7
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.i zip(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
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
3689
by: deko | last post by:
I'm kind of lost on this one - I need to modify 2 files based on user input: $data_array = file($data_file); $counter_array = file($counter_file); // There is a line-for-line relationship between the data and counter files //for example, if the 3rd line in the counter file is deleted, //so also must the 3rd line of the data file be deleted....
1
2210
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to introduce containers in xml documents even where not strictly needed? how can a recommendation on this in the light of existing tools like w3c xml schema and relaxng as well es established practice be...
0
625
by: Wolfgang Lipp | last post by:
From: Lipp, Wolfgang Sent: Tuesday, 27?January?2004 13:26 <annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with the consent of their authors. </annotation>
8
2192
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in the process), I looked up the STL code. Erase certainly does not delete the memory associated with the element. However, it appears that the...
22
3308
by: Luke | last post by:
Elements with name attribute: form, input, textarea, a, frame, iframe, button, select, map, meta, applet, object, param, img (if you know more reply...) Methods of addresing html elements: <form name="myform"> <input name="myinput" /> </form> 1. var input = document.forms.myform.myinput;//from nn3+ 2. var input =...
10
15008
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
6
2813
by: ssecorp | last post by:
GvR wants to eliminate all the functional stuff (lambda map reduce filter) which kind of makes sense for Python, listcomprehensions are more efficient(at least as implemented inpython) from what i have gathered and they can express everything map/reduce/filter with crippled lambdas can. but what about this: reduce(lambda x,y:x*y+x,) how...
37
3638
by: Prisoner at War | last post by:
Actually, it doesn't have to be a blockquote...but I'm at my wits' end: I want to make bold several lines of text which have a pair of <br /tags between them...seems like the <b></bdo not "carry over" when there are <br /tags involved...??? I've tried using <p style="font-weight: bold;"></p>, I've tried <blockquote></blockquote>...I just...
0
7779
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7781
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8064
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5291
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3732
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3732
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2208
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1040
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.