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


Groups > comp.lang.python > #31514

Re: list comprehension question

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <dihedral88888@googlemail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'subject:skip:c 10': 0.07; 'subject:question': 0.08; 'missed': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'oct': 0.16; 'reminding': 0.16; 'rings': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'import': 0.21; "i'd": 0.22; 'kevin': 0.23; 'seems': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'cc:addr:gmail.com': 0.27; 'cc:2**2': 0.27; 'correct': 0.28; 'received:209.85.212': 0.28; 'subject:list': 0.28; '>>>>': 0.29; 'faster,': 0.29; 'loop,': 0.29; "i'm": 0.29; 'maybe': 0.29; 'suggestion': 0.32; 'function.': 0.33; 'much.': 0.33; 'likely': 0.33; 'operations': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'list': 0.35; 'faster': 0.35; 'from:addr:googlemail.com': 0.35; 'said,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'cc:no real name:2**1': 0.36; 'too': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'help': 0.40; 'most': 0.61; 'real': 0.61; 'sum': 0.66; '10%': 0.81; 'improvement': 0.84; 'received:209.85.212.56': 0.91; 'rusi': 0.91; 'angel': 0.93
Newsgroups comp.lang.python
Date Wed, 17 Oct 2012 07:45:50 -0700 (PDT)
In-Reply-To <mailman.2348.1350484621.27098.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw
References <CA+C4C6f=24oD4seHY_-h1F0GMvBTWNYbzTABXOdtKkAZtE9hdQ@mail.gmail.com> <507E145E.9060008@davea.name> <CA+C4C6cjha4bMZ9gyx5fTp0c_nfFQxRGu31dGNMjgHQBu73=ig@mail.gmail.com> <mailman.2341.1350477185.27098.python-list@python.org> <61d76114-3021-4a3b-9943-1e81147d3ce1@6g2000pbh.googlegroups.com> <mailman.2348.1350484621.27098.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 123.192.32.215
MIME-Version 1.0
Subject Re: list comprehension question
From 88888 Dihedral <dihedral88888@googlemail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc rusi <rustompmody@gmail.com>, d@davea.name, python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.2349.1350485158.27098.python-list@python.org> (permalink)
Lines 77
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350485158 news.xs4all.nl 6916 [2001:888:2000:d::a6]:43158
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31514

Show key headers only | View raw


Dave Angel於 2012年10月17日星期三UTC+8下午10時37分01秒寫道:
> On 10/17/2012 10:06 AM, rusi wrote:
> 
> > On Oct 17, 5:33 pm, Dave Angel <d...@davea.name> wrote:
> 
> >> On 10/17/2012 12:43 AM, Kevin Anthony wrote:> 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.
> 
> >> list comprehensions CAN be much faster, but not necessarily.  The most
> 
> >> complex a loop, the less likely it'll help much.
> 
> > One-lining the comprehension seems to make a difference of about 10%
> 
> > out here. Maybe Ive missed something? Seems too large�
> 
> >
> 
> > # My original suggestion
> 
> > def dot(p,q): return sum (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]
> 
> >
> 
> > # One-liner (Thanks Hans for reminding me of sum)
> 
> >
> 
> > def mm1(a,b): return [[sum([x*y for x,y in zip(ra,rb)]) for rb in
> 
> > zip(*b)] for ra in a]
> 
> >
> 
> >>>> t1=Timer("res=mm1(m,m)", setup="from __main__ import mm1, m")
> 
> >>>> t1.timeit(1000)
> 
> > 12.276363849639893
> 
> >>>> t0=Timer("res=mm(m,m)", setup="from __main__ import mm, m")
> 
> >>>> t0.timeit(1000)
> 
> > 13.453603029251099
> 
> 
> 
> And I'd wager all the improvement is in the inner loop, the dot() function.
> 
> 
> 
> 
> 
> -- 
> 
> 
> 
> DaveA

Thanks for the tips of matrix operations over some fields or rings 
other than the real field and the complex field.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: list comprehension question Dave Angel <d@davea.name> - 2012-10-17 08:32 -0400
  Re: list comprehension question rusi <rustompmody@gmail.com> - 2012-10-17 07:06 -0700
    Re: list comprehension question rusi <rustompmody@gmail.com> - 2012-10-17 07:33 -0700
    Re: list comprehension question Dave Angel <d@davea.name> - 2012-10-17 10:36 -0400
      Re: list comprehension question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-17 07:45 -0700
      Re: list comprehension question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-17 07:45 -0700
      Re: list comprehension question rusi <rustompmody@gmail.com> - 2012-10-17 07:50 -0700
        Re: list comprehension question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-10-17 08:06 -0700

csiph-web