Re: list comprehension question

Discussion in 'Python' started by Terry Reedy, Oct 17, 2012.

  1. Terry Reedy

    Terry Reedy Guest

    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
     
    Terry Reedy, Oct 17, 2012
    #1
    1. Advertising

  2. Terry Reedy

    rusi Guest

    On Oct 17, 10:22 am, Terry Reedy <> 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
     
    rusi, Oct 17, 2012
    #2
    1. Advertising

  3. Terry Reedy

    Hans Mulder Guest

    On 17/10/12 09:13:57, rusi wrote:
    > On Oct 17, 10:22 am, Terry Reedy <> 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
     
    Hans Mulder, Oct 17, 2012
    #3
  4. Terry Reedy

    Terry Reedy Guest

    On 10/17/2012 3:13 AM, rusi wrote:
    > On Oct 17, 10:22 am, Terry Reedy <> 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
     
    Terry Reedy, Oct 17, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Elston

    List Comprehension question

    Mark Elston, Dec 11, 2003, in forum: Python
    Replies:
    1
    Views:
    318
    Francis Avila
    Dec 11, 2003
  2. Eelco Hoekema

    [Newby question] List comprehension

    Eelco Hoekema, Aug 6, 2004, in forum: Python
    Replies:
    7
    Views:
    282
    Eelco Hoekema
    Aug 6, 2004
  3. Shane Geiger
    Replies:
    4
    Views:
    387
    bullockbefriending bard
    Mar 25, 2007
  4. Debajit Adhikary
    Replies:
    17
    Views:
    691
    Debajit Adhikary
    Oct 18, 2007
  5. Vedran Furac(
    Replies:
    4
    Views:
    329
    Marc 'BlackJack' Rintsch
    Dec 19, 2008
Loading...

Share This Page