Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82429
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: list comprehension return a list and sum over in loop |
| Date | 2014-12-12 10:13 +0100 |
| Organization | None |
| References | <fa1c4908-8fde-424a-87af-f9741b682c37@googlegroups.com> <mailman.16893.1418368651.18130.python-list@python.org> <d03ea4ee-a361-4901-b799-d7d1c5f85ee5@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16901.1418375626.18130.python-list@python.org> (permalink) |
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()?
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