list comprehension question
Kevin Anthony
kevin.s.anthony at gmail.com
Wed Oct 17 00:43:09 EDT 2012
Is it not true that list comprehension is much faster the the for loops?
If it is not the correct way of doing this, i appoligize.
Like i said, I'm learing list comprehension.
Thanks
Kevin
On Oct 16, 2012 10:14 PM, "Dave Angel" <d at davea.name> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121017/574c3e0a/attachment.html>
More information about the Python-list
mailing list