Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82459
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <oscar.j.benjamin@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.011 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'argument': 0.05; 'error:': 0.07; 'operand': 0.09; 'rows': 0.09; 'subject:skip:c 10': 0.09; '2],': 0.16; '[2,': 0.16; 'dimension': 0.16; 'numpy': 0.16; 'to:name:python list': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'thanks.': 0.20; '>>>': 0.22; 'import': 0.22; 'this?': 0.23; 'case.': 0.24; 'example.': 0.24; 'second': 0.26; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'returned': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; 'consisting': 0.31; 'there,': 0.34; 'december': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'returning': 0.36; 'list': 0.37; 'e.g.': 0.38; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'skip:x 10': 0.40; 'length': 0.61; 'sum': 0.64; 'results': 0.69; 'oscar': 0.84; 'subject:over': 0.84; 'type(s)': 0.84 |
| 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:content-transfer-encoding; bh=LCrzL43wWPnOg+u7Xx1bG0L1KN3IAHu8cf+zbF3QaeE=; b=R6m2XSL40/GGcP1VmHo6llPmWUKTGF/6g0KJUtjfdeWFCfrDmveLRReTahE7spB/wY 3RcbNyOtIbRMKyWPuYkT/yZO+znJOTbLAZjq7Nus9bhrStJUmVHYoX2AnUBHC8QqiZNY wxxRZ+bwT+72/dz8vSUqkgNGINzZ2BkiLYE7RkVuhdn70WniPxvlf8Nl8rh+mRt1wz9H IucUC0pkB25ihnclwAFSqQvk/oXRuYsxz8ovhNdERgcCY4UJGYsQZ5gXjSZU6UXrEF/y 9x21mnk/OkWbHH8feet1MTMZcK6tZwtJWB5DIgc+yAnv6xiI2aOh9Jbao1Ybxmw3EQXj liPw== |
| X-Received | by 10.180.96.227 with SMTP id dv3mr9511873wib.50.1418402884724; Fri, 12 Dec 2014 08:48:04 -0800 (PST) |
| MIME-Version | 1.0 |
| In-Reply-To | <fa1c4908-8fde-424a-87af-f9741b682c37@googlegroups.com> |
| References | <fa1c4908-8fde-424a-87af-f9741b682c37@googlegroups.com> |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | Fri, 12 Dec 2014 16:47:43 +0000 |
| Subject | Re: list comprehension return a list and sum over in loop |
| To | Python List <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| 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 | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16920.1418402893.18130.python-list@python.org> (permalink) |
| Lines | 26 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1418402893 news.xs4all.nl 2917 [2001:888:2000:d::a6]:50500 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:82459 |
Show key headers only | View raw
On 12 December 2014 at 06:22, KK Sasa <genwei007@gmail.com> wrote: > Hi there, > > The list comprehension is results = [d2(t[k]) for k in xrange(1000)], where d2 is a function returning a list, say [x1,x2,x3,x4] for one example. So "results" is a list consisting of 1000 lists, each of length four. Here, what I want to get is the sum of 1000 lists, and then the result is a list of length four. Is there any efficient way to do this? Because I found it is slow in my case. I tried sum(d2(t[k]) for k in xrange(1000)), but it returned error: TypeError: unsupported operand type(s) for +: 'int' and 'list'. Thanks. Use numpy.sum: >>> import numpy as np >>> a = [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]] >>> np.sum(a, 0) array([6, 6, 6, 6]) >>> np.sum(a, 1) array([ 4, 8, 12]) The second argument to numpy.sum is the dimension along which you want to sum e.g. rows or columns. Oscar
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
list comprehension return a list and sum over in loop KK Sasa <genwei007@gmail.com> - 2014-12-11 22:22 -0800
Re: list comprehension return a list and sum over in loop Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-12-12 07:17 +0000
Re: list comprehension return a list and sum over in loop KK Sasa <genwei007@gmail.com> - 2014-12-12 00:30 -0800
Re: list comprehension return a list and sum over in loop Christian Gollwitzer <auriocus@gmx.de> - 2014-12-12 10:09 +0100
Re: list comprehension return a list and sum over in loop Peter Otten <__peter__@web.de> - 2014-12-12 10:13 +0100
Re: list comprehension return a list and sum over in loop KK Sasa <genwei007@gmail.com> - 2014-12-12 01:45 -0800
Re: list comprehension return a list and sum over in loop Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-12-12 13:12 +0200
Re: list comprehension return a list and sum over in loop Peter Otten <__peter__@web.de> - 2014-12-12 13:32 +0100
Re: list comprehension return a list and sum over in loop KK Sasa <genwei007@gmail.com> - 2014-12-12 05:09 -0800
Re: list comprehension return a list and sum over in loop Peter Otten <__peter__@web.de> - 2014-12-12 17:08 +0100
Re: list comprehension return a list and sum over in loop KK Sasa <genwei007@gmail.com> - 2014-12-12 19:04 -0800
Re: list comprehension return a list and sum over in loop Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-12-13 11:27 +0000
Re: list comprehension return a list and sum over in loop KK Sasa <genwei007@gmail.com> - 2014-12-12 05:03 -0800
Re: list comprehension return a list and sum over in loop Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-12 20:46 +1100
Re: list comprehension return a list and sum over in loop Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-12-12 16:47 +0000
Re: list comprehension return a list and sum over in loop pecore@pascolo.net - 2014-12-13 13:38 +0100
csiph-web