<p>Is it not true that list comprehension is much faster the the for loops?</p>
<p>If it is not the correct way of doing this, i appoligize.  <br>
Like i said, I'm learing list comprehension.</p>
<p>Thanks<br>
Kevin</p>
<div class="gmail_quote">On Oct 16, 2012 10:14 PM, "Dave Angel" <<a href="mailto:d@davea.name">d@davea.name</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 10/16/2012 09:54 PM, Kevin Anthony wrote:<br>
> I've been teaching myself list comprehension, and i've run across something<br>
> i'm not able to convert.<br>
><br>
> here's the original code for matrix multiplcation<br>
><br>
> retmatrix = Matrix(self.__row,other.__col)<br>
> for m in range(0,retmatrix.__row):<br>
>     for n in range(0,retmatrix.__col):<br>
>         product = 0<br>
>         for p in range(1,self.__col+1):<br>
>             product += (self.__matrix[m][p] * other.__matrix[p][n])<br>
>         retmatrix.__matrix[m][n] = product<br>
><br>
> Here is what i have so far:<br>
> retmatrix.__matrix = [[ product = product + (self.__matrix[m][p]*<br>
> other.__matrix[p][n])<br>
>                  if product else self.__matrix[m][p]* other.__matrix[p][n])<br>
>                  for p in range(0,self.col)<br>
>                  for n in range(0,self.col)]<br>
>                  for m in range(0,self.__row)]<br>
><br>
> But i know that isn't correct, can someone nudge my in the right direction?<br>
><br>
><br>
<br>
The biggest thing to learn about list comprehensions is when not to use<br>
them.  I can't imagine how your latter version (even if correct) is<br>
clearer than the first.<br>
<br>
<br>
<br>
--<br>
<br>
DaveA<br>
<br>
</blockquote></div>