Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82477

Re: list comprehension return a list and sum over in loop

From pecore@pascolo.net
Newsgroups comp.lang.python
Subject Re: list comprehension return a list and sum over in loop
Date 2014-12-13 13:38 +0100
Organization The Sun and the Rain.
Message-ID <87zjar3esi.fsf@pascolo.net> (permalink)
References <fa1c4908-8fde-424a-87af-f9741b682c37@googlegroups.com>

Show all headers | View raw


KK Sasa <genwei007@gmail.com> writes:

> 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.

Why didn't you  follow Mark Lawrence's advice? 

In your problem, results is a list of N sublists, each containing
exactly four numerical values,

Let's try with N=2

In [36]: results = [d2(t[k]) for k in range(2)]
In [37]: print results
[[1, 2, 3, 4], [5, 6, 7, 8]]

Let's try the obvious method to sum

In [38]: [sum(el) for el in results]
Out[38]: [10, 26]

not what you're looking for, but what if we had 

In [39]: [sum(el) for el in zip(*results)]
Out[39]: [6, 8, 10, 12]

correct.

BTW, as you're using the scientific stack the answer of Christian
Gollwitzer is the more appropriate.

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


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