Re: list comprehension question

Discussion in 'Python' started by Dave Angel, Oct 17, 2012.

  1. Dave Angel

    Dave Angel Guest

    On 10/16/2012 09:54 PM, Kevin Anthony wrote:
    > I've been teaching myself list comprehension, and i've run across something
    > i'm not able to convert.
    >
    > here's the original code for matrix multiplcation
    >
    > retmatrix = Matrix(self.__row,other.__col)
    > for m in range(0,retmatrix.__row):
    > for n in range(0,retmatrix.__col):
    > product = 0
    > for p in range(1,self.__col+1):
    > product += (self.__matrix[m][p] * other.__matrix[p][n])
    > retmatrix.__matrix[m][n] = product
    >
    > Here is what i have so far:
    > retmatrix.__matrix = [[ product = product + (self.__matrix[m][p]*
    > other.__matrix[p][n])
    > if product else self.__matrix[m][p]* other.__matrix[p][n])
    > for p in range(0,self.col)
    > for n in range(0,self.col)]
    > for m in range(0,self.__row)]
    >
    > But i know that isn't correct, can someone nudge my in the right direction?
    >
    >


    The biggest thing to learn about list comprehensions is when not to use
    them. I can't imagine how your latter version (even if correct) is
    clearer than the first.



    --

    DaveA
     
    Dave Angel, Oct 17, 2012
    #1
    1. Advertising

  2. Dave Angel

    rusi Guest

    On Oct 17, 7:14 am, Dave Angel <> wrote:
    > On 10/16/2012 09:54 PM, Kevin Anthony wrote:
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > > I've been teaching myself list comprehension, and i've run across something
    > > i'm not able to convert.

    >
    > > here's the original code for matrix multiplcation

    >
    > > retmatrix = Matrix(self.__row,other.__col)
    > > for m in range(0,retmatrix.__row):
    > >     for n in range(0,retmatrix.__col):
    > >         product = 0
    > >         for p in range(1,self.__col+1):
    > >             product += (self.__matrix[m][p] * other.__matrix[p][n])
    > >         retmatrix.__matrix[m][n] = product

    >
    > > Here is what i have so far:
    > > retmatrix.__matrix = [[ product = product + (self.__matrix[m][p]*
    > > other.__matrix[p][n])
    > >                  if product else self.__matrix[m][p]*other.__matrix[p][n])
    > >                  for p in range(0,self.col)
    > >                  for n in range(0,self.col)]
    > >                  for m in range(0,self.__row)]

    >
    > > But i know that isn't correct, can someone nudge my in the right direction?

    >
    > The biggest thing to learn about list comprehensions is when not to use
    > them.  I can't imagine how your latter version (even if correct) is
    > clearer than the first.
    >
    > --
    >
    > DaveA


    Try rewriting using dot

    from operator import mul
    def dot(p,q):
    return reduce(mul, [x*y for x,y in zip(p,q)]) # the [] can become
    ()

    and avoiding object-orientation (at least to start with)
     
    rusi, Oct 17, 2012
    #2
    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:
    333
    Francis Avila
    Dec 11, 2003
  2. Eelco Hoekema

    [Newby question] List comprehension

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

Share This Page