Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sum for sequences?

Reply
Thread Tools

sum for sequences?

 
 
kj
Guest
Posts: n/a
 
      03-24-2010


Is there a sequence-oriented equivalent to the sum built-in? E.g.:

seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)

?

(By "sequence" I'm referring primarily to lists and tuples, and
excluding strings, since for these there is ''.join()).

TIA!

~K
 
Reply With Quote
 
 
 
 
Glazner
Guest
Posts: n/a
 
      03-24-2010
On Mar 24, 5:29*pm, kj <(E-Mail Removed)> wrote:
> Is there a sequence-oriented equivalent to the sum built-in? *E.g.:
>
> * seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>
> ?
>
> (By "sequence" I'm referring primarily to lists and tuples, and
> excluding strings, since for these there is ''.join()).
>
> TIA!
>
> ~K


try itertools.chain
 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      03-24-2010
On 2010-03-24, kj <(E-Mail Removed)> wrote:
>
>
> Is there a sequence-oriented equivalent to the sum built-in? E.g.:
>
> seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>
> ?
>
> (By "sequence" I'm referring primarily to lists and tuples, and
> excluding strings, since for these there is ''.join()).


reduce, or functools.reduce in Python 3.1.

>>> functools.reduce(operator.add, ((1, 2), (5, 6)))

(1, 2, 5, 6)

--
Neil Cerutti
"It's not fun to build walls. But it's even less fun to live
without walls in a world full of zombies." --Greedy Goblin
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-24-2010
kj wrote:
>
> Is there a sequence-oriented equivalent to the sum built-in? E.g.:
>
> seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>
> ?
>
> (By "sequence" I'm referring primarily to lists and tuples, and
> excluding strings, since for these there is ''.join()).
>

Do you mean you want to flatten a list structure? There have been
several discussions about this, which Google will find for you quite easily.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pycon.blip.tv/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-24-2010
On Wed, 24 Mar 2010 15:29:07 +0000, kj wrote:

> Is there a sequence-oriented equivalent to the sum built-in? E.g.:
>
> seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>
> ?


Yes, sum.

help(sum) is your friend.

>>> a = range(2)
>>> b = range(3)
>>> c = range(4)
>>> sum((a, b, c), [])

[0, 1, 0, 1, 2, 0, 1, 2, 3]


Beware though that sum on lists and tuples will be fairly inefficient if
you have lots of them. You may find that this will be much more efficient:

result = []
for seq in sequences:
result.extend(seq)



--
Steven
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      03-24-2010
kj <(E-Mail Removed)> writes:
> Is there a sequence-oriented equivalent to the sum built-in? E.g.:
> seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)


use itertools.chain for this. A few people have mentioned that sum will
also work, but I think for that purpose it could have O(n**2)
complexity.
 
Reply With Quote
 
TomF
Guest
Posts: n/a
 
      03-25-2010
On 2010-03-24 14:07:24 -0700, Steven D'Aprano
<(E-Mail Removed)> said:
> On Wed, 24 Mar 2010 15:29:07 +0000, kj wrote:
>
>> Is there a sequence-oriented equivalent to the sum built-in? E.g.:
>>
>> seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>>
>> ?

>
> Yes, sum.
>
> help(sum) is your friend.


You might not want to be so glib. The sum doc sure doesn't sound like
it should work on lists.

Returns the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0).

-Tom

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      03-25-2010
On Wed, 24 Mar 2010 23:50:23 -0700, TomF wrote:

> On 2010-03-24 14:07:24 -0700, Steven D'Aprano
> <(E-Mail Removed)> said:
>> On Wed, 24 Mar 2010 15:29:07 +0000, kj wrote:
>>
>>> Is there a sequence-oriented equivalent to the sum built-in? E.g.:
>>>
>>> seq_sum(((1, 2), (5, 6))) --> (1, 2) + (5, 6) --> (1, 2, 5, 6)
>>>
>>> ?

>>
>> Yes, sum.
>>
>> help(sum) is your friend.

>
> You might not want to be so glib. The sum doc sure doesn't sound like
> it should work on lists.
>
> Returns the sum of a sequence of numbers (NOT strings) plus the
> value of parameter 'start' (which defaults to 0).



What part of that suggested to you that sum might not be polymorphic?
Sure, it says numbers (which should be changed, in my opinion), but it
doesn't specify what sort of numbers -- ints, floats, or custom types
that have an __add__ method. It also singles out strings as excluded. Why
would you need to explicitly exclude strings, since they're not numbers,
if sum *only* works with numbers?

E.g. help(math.sin) could have said this, but doesn't:

Return the sine of x (NOT a dictionary)

It doesn't need to, because dicts aren't exceptional: sin doesn't work on
anything *but* numbers. There's no __sin__ method to call on arbitrary
types.

The fact that sum does single out strings is a clear sign that strings
are treated as exceptional and suggests strongly that summing arbitrary
types should work. I'm not saying that help(sum) explicitly states that
it works with lists (it clearly doesn't), but it does suggest the
possibility and makes the experiment worth trying.

I'll also note that the Fine Manual makes it even more clear that sum is
polymorphic:

http://docs.python.org/library/functions.html#sum




--
Steven
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      03-25-2010
On 2010-03-25, Steven D'Aprano <(E-Mail Removed)> wrote:
>> You might not want to be so glib. The sum doc sure doesn't
>> sound like it should work on lists.
>>
>> Returns the sum of a sequence of numbers (NOT strings) plus the
>> value of parameter 'start' (which defaults to 0).

>
> What part of that suggested to you that sum might not be polymorphic?
> Sure, it says numbers (which should be changed, in my opinion), but it
> doesn't specify what sort of numbers -- ints, floats, or custom types
> that have an __add__ method.


WTF.

--
Neil Cerutti
"It's not fun to build walls. But it's even less fun to live
without walls in a world full of zombies." --Greedy Goblin
 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      03-25-2010
Neil Cerutti, 25.03.2010 13:37:
> On 2010-03-25, Steven D'Aprano wrote:
>>> You might not want to be so glib. The sum doc sure doesn't
>>> sound like it should work on lists.
>>>
>>> Returns the sum of a sequence of numbers (NOT strings) plus the
>>> value of parameter 'start' (which defaults to 0).

>>
>> What part of that suggested to you that sum might not be polymorphic?
>> Sure, it says numbers (which should be changed, in my opinion), but it
>> doesn't specify what sort of numbers -- ints, floats, or custom types
>> that have an __add__ method.

>
> WTF.


Warning: truth found!

Stefan

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packet retransmits and bad check sum on ethereal capture Newbie72 Cisco 4 03-10-2006 01:52 PM
Datagrid sort & sum question ASP .Net 1 07-31-2004 12:15 PM
Calculate Running sum of 4 text boxes on client side script? Davisro ASP .Net 2 04-20-2004 08:18 PM
Javascript into ASP.Net Simple example wanted.. Sum Textboxes client side Davisro ASP .Net 4 04-10-2004 08:42 AM
DataGrid: how to have a footer row dynamically sum data Roger Moore ASP .Net 0 07-07-2003 05:27 PM



Advertisments