Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29497
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'defaults': 0.05; "'a'": 0.07; 'operand': 0.07; 'parameter': 0.07; 'strings.': 0.07; 'python': 0.09; 'empty,': 0.09; 'restriction': 0.09; 'sep': 0.09; 'typeerror:': 0.09; "'b'": 0.16; "'b',": 0.16; "'c'": 0.16; "'c',": 0.16; "'d',": 0.16; "'e',": 0.16; "'int'": 0.16; '0).': 0.16; 'efficiency.': 0.16; 'instead:': 0.16; 'unsupported': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; '>>>': 0.18; '(not': 0.20; 'equivalent': 0.20; 'doc': 0.22; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'expand': 0.26; 'am,': 0.27; 'wonder': 0.27; 'message-id:@mail.gmail.com': 0.27; '>>>>': 0.29; "skip:' 10": 0.30; 'notes': 0.30; 'error': 0.30; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'sequence': 0.35; 'received:209.85': 0.35; 'add': 0.36; 'should': 0.36; 'subject: (': 0.36; 'does': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'hello,': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'sum': 0.66; 'start.': 0.84; 'strings)': 0.84; 'to:name:python': 0.84; 'type(s)': 0.84; 'inefficient': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=/BYPxR3nsAdmK2MsSM7W7TUCueV/WukFrHfAEp/t1sM=; b=scSot0ASiHQiQrMf17w3i9pokOX3ONPOFOiNT36R+aT/AQF6v0OoCuS4JADgPrDyMD mPUDmF5Jtu/Tlm0DTmNizfvTN6meFWRwtrKzGUVjeJ4jw4DZlpobo39so3czvvKBoLrY +51e31rHJM+q4t+8jrsG/pEk6tTQ+hmba9YxKy7ENbzxW0+Qd+rAObnOv/ixh/gYIG0m f1ZlJxkBYOa+cogllgHky7YZtgtwCFhiv9360v8N+XkaL9sa6Rsnh5K2nkvX7nh2NoK4 mBkih7WDc+/4XiB2m1J5J/Xigvgk89H8xXyIc1qZlrrZlpvJ/qfJ+tFED0xmqhOrWRsq kXCw== |
| MIME-Version | 1.0 |
| In-Reply-To | <franck-E8B1EB.16412019092012@news.free.fr> |
| References | <franck-E8B1EB.16412019092012@news.free.fr> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Wed, 19 Sep 2012 09:03:03 -0600 |
| Subject | Re: sum works in sequences (Python 3) |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.920.1348067016.27098.python-list@python.org> (permalink) |
| Lines | 30 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1348067016 news.xs4all.nl 6986 [2001:888:2000:d::a6]:43561 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:29497 |
Show key headers only | View raw
On Wed, Sep 19, 2012 at 8:41 AM, Franck Ditter <franck@ditter.org> wrote:
> Hello,
> I wonder why sum does not work on the string sequence in Python 3 :
>
>>>> sum((8,5,9,3))
> 25
>>>> sum([5,8,3,9,2])
> 27
>>>> sum('rtarze')
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>
> I naively thought that sum('abc') would expand to 'a'+'b'+'c'
> And the error message is somewhat cryptic...
It notes in the doc string that it does not work on strings:
sum(...)
sum(sequence[, start]) -> value
Returns the sum of a sequence of numbers (NOT strings) plus the value
of parameter 'start' (which defaults to 0). When the sequence is
empty, returns start.
I think this restriction is mainly for efficiency. sum(['a', 'b',
'c', 'd', 'e']) would be the equivalent of 'a' + 'b' + 'c' + 'd' +
'e', which is an inefficient way to add together strings. You should
use ''.join instead:
>>> ''.join('abc')
'abc'
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
sum works in sequences (Python 3) Franck Ditter <franck@ditter.org> - 2012-09-19 16:41 +0200
Re: sum works in sequences (Python 3) Joel Goldstick <joel.goldstick@gmail.com> - 2012-09-19 10:57 -0400
Re: sum works in sequences (Python 3) Neil Cerutti <neilc@norwich.edu> - 2012-09-19 14:57 +0000
Re: sum works in sequences (Python 3) Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-19 09:03 -0600
Re: sum works in sequences (Python 3) Neil Cerutti <neilc@norwich.edu> - 2012-09-19 15:06 +0000
Re: sum works in sequences (Python 3) Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-19 09:24 -0600
Re: sum works in sequences (Python 3) Steve Howell <showell30@yahoo.com> - 2012-09-19 08:37 -0700
Re: sum works in sequences (Python 3) Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-19 12:33 -0600
Re: sum works in sequences (Python 3) Steve Howell <showell30@yahoo.com> - 2012-09-19 11:43 -0700
Re: sum works in sequences (Python 3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-19 16:14 +0000
Re: sum works in sequences (Python 3) Alister <alister.ware@ntlworld.com> - 2012-09-19 15:07 +0000
Re: sum works in sequences (Python 3) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-09-19 16:18 +0000
Re: sum works in sequences (Python 3) Terry Reedy <tjreedy@udel.edu> - 2012-09-19 14:49 -0400
Re: sum works in sequences (Python 3) Hans Mulder <hansmu@xs4all.nl> - 2012-09-20 00:25 +0200
csiph-web