sum and strings

Paddy paddy3118 at netscape.net
Sun Aug 20 05:44:03 EDT 2006


Rhamphoryncus wrote:

>
> It's worthwhile to note that the use of + as the concatenation operator
> is arbitrary.  It could just have well been | or &, and has no
> relationship with mathematically addition.

The effect of the string concatenation operator is only secondary.
Secondary to the use of the word sum; and what could be 'reasonably'
concieved as sum's effect on  non-numeric types.
>
> It's also worth stressing (not in response to your post, but others)
> that sum([[1],[2],[3]], []) is just as bad as attempting to sum
> strings, both conceptually (it's not mathematical addition)

Unfortunately, the words sum and summation are linked to other words
that are commonly used to describe what is happening to the numders,
the lists, and the strings.
Words such as accumulate, concatenate, aggregate, collect, assemble, as
well as add.

> and performance-wise.  Don't do it. :)

Amen to that.

> I believe the prefered method to flatten a list of lists is this:
>
> shallow = []
> for i in deep:
>     shallow.extend(i)
>
> Yes, it's three lines.  It's also very easy to read.  reduce() and
> sum() are not.

I'd like to squeeze in the listcomp version, not because it is one line
shorter, but because I, and maybe others prefer short listcomps such as
the folowing:

shallow = []
[shallow.extend(i) for i in deep]

-Pad.




More information about the Python-list mailing list