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: 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: References: From: Oscar Benjamin Date: Fri, 12 Dec 2014 16:47:43 +0000 Subject: Re: list comprehension return a list and sum over in loop To: Python List 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On 12 December 2014 at 06:22, KK Sasa wrote: > Hi there, > > The list comprehension is results =3D [d2(t[k]) for k in xrange(1000)], w= here 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 ret= urned error: TypeError: unsupported operand type(s) for +: 'int' and 'list'= . Thanks. Use numpy.sum: >>> import numpy as np >>> a =3D [[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