On 6/7/07, David Joyner <[EMAIL PROTECTED]> wrote: > > though for some reason it doesn't work on strings: > > sage: sum(["S","A","G","E"]) > --------------------------------------------------------------------------- > <type 'exceptions.TypeError'> Traceback (most recent call last) > > /mnt/hd200/sagefiles/sage-2.5.alpha2/<ipython console> in <module>() > > <type 'exceptions.TypeError'>: unsupported operand type(s) for +: > 'int' and 'str'
The Python documentation for sum very specifically disallows summing strings for mysterious reasons: sum(sequence, start=0) -> value Returns the sum of a sequence of numbers (NOT strings) plus the value of parameter 'start'. When the sequence is empty, returns start. ---- With sum you often have to give the start value, but with strings that doesn't work: sage: sum(['S', 'A', 'G', 'E'], '') --------------------------------------------------------------------------- <type 'exceptions.TypeError'> Traceback (most recent call last) /Users/was/<ipython console> in <module>() <type 'exceptions.TypeError'>: sum() can't sum strings [use ''.join(seq) instead] sage: ----- I think it must be some sort of efficiency issue, where ''.join(...) is much faster, or something. william --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~----------~----~----~----~------~----~------~--~---