Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #29498
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Neil Cerutti <neilc@norwich.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: sum works in sequences (Python 3) |
| Date | 19 Sep 2012 15:06:43 GMT |
| Organization | Norwich University |
| Lines | 36 |
| Message-ID | <abu5c3FblbqU2@mid.individual.net> (permalink) |
| References | <franck-E8B1EB.16412019092012@news.free.fr> <mailman.920.1348067016.27098.python-list@python.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 8bit |
| X-Trace | individual.net xjXV21wOa3TMlNK5jihDBAfV8lqkzUJm0F3Y2HNvxfM3CWL3T9hCXSTuzbRv5GOmEw |
| Cancel-Lock | sha1:TjCMMhcg0TSNUbjD0OszpC7BI/M= |
| User-Agent | slrn/0.9.9p1/mm/ao (Win32) |
| Xref | csiph.com comp.lang.python:29498 |
Show key headers only | View raw
On 2012-09-19, Ian Kelly <ian.g.kelly@gmail.com> wrote: > 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: While the docstring is still useful, it has diverged from the documentation a little bit. sum(iterable[, start]) Sums start and the items of an iterable from left to right and returns the total. start defaults to 0. The iterable‘s items are normally numbers, and the start value is not allowed to be a string. For some use cases, there are good alternatives to sum(). The preferred, fast way to concatenate a sequence of strings is by calling ''.join(sequence). To add floating point values with extended precision, see math.fsum(). To concatenate a series of iterables, consider using itertools.chain(). Are iterables and sequences different enough to warrant posting a bug report? -- Neil Cerutti
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