Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31540
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; '*not*': 0.07; 'append': 0.07; 'nested': 0.07; 'repeated': 0.07; 'subject:skip:c 10': 0.07; 'subject:question': 0.08; 'combines': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'res': 0.09; 'terry': 0.09; 'def': 0.10; 'essential': 0.15; 'loops': 0.16; 'oct': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'relevant.': 0.16; 'wrote:': 0.17; 'helper': 0.17; 'jan': 0.18; '>>>': 0.18; 'equivalent': 0.20; 'otherwise,': 0.20; 'trying': 0.21; 'import': 0.21; 'converted': 0.22; 'either.': 0.22; 'kevin': 0.23; "i've": 0.23; 'specifically': 0.24; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'fit': 0.26; 'am,': 0.27; 'header:X -Complaints-To:1': 0.28; 'subject:list': 0.28; 'run': 0.28; 'about.': 0.29; 'attempted': 0.29; 'implied': 0.29; 'sensible': 0.29; 'vector': 0.29; 'convert': 0.29; "i'm": 0.29; 'that.': 0.30; 'function': 0.30; 'code': 0.31; 'point': 0.31; 'could': 0.32; 'to:addr:python-list': 0.33; '(with': 0.33; 'produced': 0.33; 'that,': 0.34; 'done': 0.34; 'list': 0.35; 'said,': 0.35; 'pm,': 0.35; 'something': 0.35; 'add': 0.36; 'received:org': 0.36; 'explain': 0.36; 'but': 0.36; 'should': 0.36; 'does': 0.37; 'why': 0.37; 'item': 0.37; 'rather': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'different': 0.63; 'difficulty': 0.65; 'talking': 0.66; 'teaching': 0.66; 'products': 0.70; 'multiply': 0.84; 'received:fios.verizon.net': 0.84; 'trickery': 0.84; 'rusi': 0.91; 'sensibly': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: list comprehension question |
| Date | Wed, 17 Oct 2012 17:46:09 -0400 |
| References | <CA+C4C6f=24oD4seHY_-h1F0GMvBTWNYbzTABXOdtKkAZtE9hdQ@mail.gmail.com> <mailman.2321.1350451320.27098.python-list@python.org> <ee56557d-4450-4308-8488-8e2b6c1f9bc3@a4g2000pbo.googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 |
| In-Reply-To | <ee56557d-4450-4308-8488-8e2b6c1f9bc3@a4g2000pbo.googlegroups.com> |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2363.1350510396.27098.python-list@python.org> (permalink) |
| Lines | 66 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1350510396 news.xs4all.nl 6881 [2001:888:2000:d::a6]:53383 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:31540 |
Show key headers only | View raw
On 10/17/2012 3:13 AM, rusi wrote: > On Oct 17, 10:22 am, Terry Reedy <tjre...@udel.edu> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: list comprehension question Terry Reedy <tjreedy@udel.edu> - 2012-10-17 01:21 -0400
Re: list comprehension question rusi <rustompmody@gmail.com> - 2012-10-17 00:13 -0700
Re: list comprehension question Hans Mulder <hansmu@xs4all.nl> - 2012-10-17 10:34 +0200
Re: list comprehension question Terry Reedy <tjreedy@udel.edu> - 2012-10-17 17:46 -0400
csiph-web