Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!82.197.223.108.MISMATCH!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'error:': 0.07; '[0]': 0.09; 'lawrence': 0.09; 'operand': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:skip:c 10': 0.09; 'language.': 0.14; 'enough.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'typeerror:': 0.16; 'language': 0.16; 'wrote:': 0.18; 'addition,': 0.20; 'thanks.': 0.20; 'help.': 0.21; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'example.': 0.24; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'correct': 0.29; 'wonder': 0.29; 'returned': 0.30; 'subject:list': 0.30; "i'm": 0.30; 'reply.': 0.31; 'consisting': 0.31; 'lists': 0.32; 'probably': 0.32; 'there,': 0.34; 'problem': 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'returning': 0.36; "didn't": 0.36; 'method': 0.36; 'thanks': 0.36; "i'll": 0.36; 'list': 0.37; 'clear': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'tell': 0.60; 'length': 0.61; 'kind': 0.63; 'our': 0.64; 'sum': 0.64; 'results': 0.69; 'subject:over': 0.84; 'type(s)': 0.84; '8bit%:70': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: list comprehension return a list and sum over in loop Date: Fri, 12 Dec 2014 10:13:32 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bd8af7.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1418375626 news.xs4all.nl 2957 [2001:888:2000:d::a6]:55770 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:82429 KK Sasa wrote: > Mark Lawrence於 2014年12月12日星期五UTC+8下午3時17分43秒寫道: >> On 12/12/2014 06:22, KK Sasa 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. >> > >> >> I think you need something like this >> http://stackoverflow.com/questions/19339/a-transpose-unzip-function-in-python-inverse-of-zip >> >> I'll let you add the finishing touches if I'm correct :) >> >> -- >> My fellow Pythonistas, ask not what our language can do for you, ask >> what you can do for our language. >> >> Mark Lawrence > > Hi Mark and Yotam, > Thanks for kind reply. I think I didn't make my problem clear enough. > The slow part is "[d2(t[k]) for k in xrange(1000)]". In addition, I > don't need to construct a list of 1000 lists inside, but my aim is to > get the sum of all "d2(t[k])". I wonder if there is any method to sum up > efficiently. If that is slow the culprit is probably the d2() function. If so results = [0] * 4 for k in xrange(1000): for i, v in enumerate(d2(t[k])): results[i] += v won't help. Can you tell us what's inside d2()?