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


Groups > comp.lang.python > #31466 > unrolled thread

Re: list comprehension question

Started byTerry Reedy <tjreedy@udel.edu>
First post2012-10-17 01:21 -0400
Last post2012-10-17 17:46 -0400
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: list comprehension question Terry Reedy <tjreedy@udel.edu> - 2012-10-17 01:21 -0400
    Re: list comprehension question rusi <rustompmody@gmail.com> - 2012-10-17 00:13 -0700
      Re: list comprehension question Hans Mulder <hansmu@xs4all.nl> - 2012-10-17 10:34 +0200
      Re: list comprehension question Terry Reedy <tjreedy@udel.edu> - 2012-10-17 17:46 -0400

#31466 — Re: list comprehension question

FromTerry Reedy <tjreedy@udel.edu>
Date2012-10-17 01:21 -0400
SubjectRe: list comprehension question
Message-ID<mailman.2321.1350451320.27098.python-list@python.org>
On 10/16/2012 9:54 PM, Kevin Anthony wrote:
> I've been teaching myself list comprehension, and i've run across
> something i'm not able to convert.

list comprehensions specifically abbreviate the code that they are 
(essentially) equivalent to.

res = []
for item in source:
   res.append(f(item))
res

<==>

[f(item) for item in source]

Matrix multiplication does not fit the pattern above. The reduction is 
number addition rather than list appending.

-- 
Terry Jan Reedy

[toc] | [next] | [standalone]


#31478

Fromrusi <rustompmody@gmail.com>
Date2012-10-17 00:13 -0700
Message-ID<ee56557d-4450-4308-8488-8e2b6c1f9bc3@a4g2000pbo.googlegroups.com>
In reply to#31466
On Oct 17, 10:22 am, Terry Reedy <tjre...@udel.edu> wrote:
> On 10/16/2012 9:54 PM, Kevin Anthony wrote:
>
> > I've been teaching myself list comprehension, and i've run across
> > something i'm not able to convert.
>
> list comprehensions specifically abbreviate the code that they are
> (essentially) equivalent to.
>
> res = []
> for item in source:
>    res.append(f(item))
> res
>
> <==>
>
> [f(item) for item in source]
>
> Matrix multiplication does not fit the pattern above. The reduction is
> number addition rather than list appending.

Dunno why you say that. Heres matrix multiply using list
comprehensions:

from operator import add
def dot(p,q): return reduce(add, (x*y for x,y in zip(p,q)))

def transpose(m): return zip(*m)

def mm(a,b): return mmt(a, transpose(b))

def mmt(a,b): return [[dot(ra, rb) for rb in b] for ra in a]

which can then be 'reduced' to a one-liner if that takes your fancy

[toc] | [prev] | [next] | [standalone]


#31487

FromHans Mulder <hansmu@xs4all.nl>
Date2012-10-17 10:34 +0200
Message-ID<507e6d8e$0$6851$e4fe514c@news2.news.xs4all.nl>
In reply to#31478
On 17/10/12 09:13:57, rusi wrote:
> On Oct 17, 10:22 am, Terry Reedy <tjre...@udel.edu> wrote:
>> On 10/16/2012 9:54 PM, Kevin Anthony wrote:
>>
>>> I've been teaching myself list comprehension, and i've run across
>>> something i'm not able to convert.
>>
>> list comprehensions specifically abbreviate the code that they are
>> (essentially) equivalent to.
>>
>> res = []
>> for item in source:
>>    res.append(f(item))
>> res
>>
>> <==>
>>
>> [f(item) for item in source]
>>
>> Matrix multiplication does not fit the pattern above. The reduction is
>> number addition rather than list appending.
> 
> Dunno why you say that. Heres matrix multiply using list
> comprehensions:
> 
> from operator import add
> def dot(p,q): return reduce(add, (x*y for x,y in zip(p,q)))
> 
> def transpose(m): return zip(*m)
> 
> def mm(a,b): return mmt(a, transpose(b))
> 
> def mmt(a,b): return [[dot(ra, rb) for rb in b] for ra in a]
> 
> which can then be 'reduced' to a one-liner if that takes your fancy

I can golf it down to two lines without losing readability:

def dot(p,q): return sum(x*y for x,y in zip(p,q))

def mm(a,b): return [[dot(ra, rb) for rb in zip(*b)] for ra in a]


Hope this helps,

-- HansM

[toc] | [prev] | [next] | [standalone]


#31540

FromTerry Reedy <tjreedy@udel.edu>
Date2012-10-17 17:46 -0400
Message-ID<mailman.2363.1350510396.27098.python-list@python.org>
In reply to#31478
On 10/17/2012 3:13 AM, rusi wrote:
> On Oct 17, 10:22 am, Terry Reedy <tjre...@udel.edu> wrote:
>> On 10/16/2012 9:54 PM, Kevin Anthony wrote:
>>
>>> I've been teaching myself list comprehension, and i've run across
>>> something i'm not able to convert.

My response is to the part Kevin could *not* convert, not the parts he 
did convert. I attempted to explain why he could not convert that part.

>> list comprehensions specifically abbreviate the code that they are
>> (essentially) equivalent to.
>>
>> res = []
>> for item in source:
>>     res.append(f(item))
>> res
>>
>> <==>
>>
>> [f(item) for item in source]
>>
>> Matrix multiplication does not fit the pattern above. The reduction is
>> number addition rather than list appending.
>
> Dunno why you say that.

Because it is true and because it makes an essential point about what 
one can and cannot sensibly do with comprehensions. They are not 
intended to be a replacement for *all* loops.

The essential inner reduction by addition of products that Kevin was 
'not able to convert' cannot be converted (with out some obnoxious 
trickery and *some* extra helper), so his request for a sensible 
conversion is futile.

> Heres matrix multiply using list comprehensions:

plus a helper function that does the inner reduction otherwise, as I 
implied it should be

> from operator import add
> def dot(p,q): return reduce(add, (x*y for x,y in zip(p,q)))

Right, this is the addition reduction that the OP was trying to
convert to a list comp. It cannot be done and you have not done it 
either. Note the the vector of products is produced as a comprehension. 
That you left it as a 'generator expression' is not relevant.

The important point is the the addition combines the products of 
different iterations and list comps, by their nature, cannot directly do 
that.

> def transpose(m): return zip(*m)
>
> def mm(a,b): return mmt(a, transpose(b))
>
> def mmt(a,b): return [[dot(ra, rb) for rb in b] for ra in a]

This is the repeated append part of the original nested loops and that, 
as I said, can be re-expressed as a list comp. But that was not the part 
Kevin was having difficulty with and not the part I was talking about.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web