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


Groups > comp.lang.python > #32933

Re: Multi-dimensional list initialization

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'example:': 0.03; 'interpreter': 0.04; '(even': 0.05; 'cache': 0.05; 'case.': 0.05; 'context': 0.05; 'correct.': 0.07; 'differently': 0.07; 'indexing': 0.07; 'indices': 0.07; 'parameter': 0.07; 'repeated': 0.07; 'python': 0.09; '(instead': 0.09; 'broken.': 0.09; 'cached': 0.09; 'parsed': 0.09; 'semantics': 0.09; 'thread,': 0.09; 'stored': 0.10; 'files.': 0.13; 'index': 0.13; "(i'm": 0.16; '(people': 0.16; 'bracket': 0.16; 'caches': 0.16; 'caching': 0.16; 'comma': 0.16; 'constructs': 0.16; 'devs': 0.16; 'fond': 0.16; 'fyi,': 0.16; 'integer.': 0.16; 'interpreter;': 0.16; 'invokes': 0.16; 'keywords.': 0.16; 'lambda': 0.16; 'list;': 0.16; 'sequence.': 0.16; 'slice.': 0.16; 'slicing,': 0.16; 'xrange': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'saying': 0.18; 'variable': 0.20; 'equivalent': 0.20; 'respective': 0.20; 'suggested': 0.20; 'meant': 0.21; 'latter': 0.22; 'operations.': 0.22; 'subject:skip:i 10': 0.22; 'needed.': 0.23; 'specified': 0.23; 'raise': 0.24; 'idea': 0.24; 'second': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'common': 0.26; 'values': 0.26; 'andrew': 0.27; 'execution': 0.27; 'instead.': 0.27; 'object,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'skip:( 20': 0.28; 'subject:list': 0.28; 'once,': 0.29; 'reduced': 0.29; 'probably': 0.29; "i'm": 0.29; 'related': 0.30; 'keyword': 0.30; 'received:209.85.215.46': 0.30; 'basic': 0.30; 'function': 0.30; 'error': 0.30; 'code': 0.31; 'url:python': 0.32; 'file': 0.32; 'function.': 0.33; 'traditional': 0.33; 'utility': 0.33; 'to:addr :python-list': 0.33; 'times.': 0.33; 'that,': 0.34; 'received:google.com': 0.34; 'done': 0.34; 'list': 0.35; 'nov': 0.35; 'sequence': 0.35; 'so,': 0.35; 'doing': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'list.': 0.35; 'created': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'method': 0.36; 'anything': 0.36; 'ok,': 0.37; 'uses': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'nothing': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'subject:-': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'range': 0.60; 'map': 0.61; 'first': 0.61; 'time,': 0.62; 'evaluate': 0.62; 'different': 0.63; 'times': 0.63; 'taking': 0.65; 'frequently': 0.65; 'contact': 0.68; 'construction': 0.72; 'special': 0.73; 'case?': 0.84; 'complaint': 0.84; 'ini': 0.84; 'start.': 0.84; 'stop,': 0.84; 'to:name:python': 0.84; 'url:functions': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=cJmyBulgYGpXclgJwPImVwDvmRuKiaQg1kSoj8x0ozM=; b=NfwHchx1mpwjIMZRe/XyCREMwu5UJKcrT00IdEuJZDpAIDm+bfqnOqGkChTnY6AUkT m2VlSmBEvBO91uHcPfYUooaeky4gNP+9p1hjbLEiQlmdrikIXqj3gdeESsRMWkVKq2Hr vLSzMBY1VrAl/w5JoT/K+BiWAsTUrcPCYXDiSNVzUiL5Nc3fLNCr8FOcdzDDHCDdOYAZ we4U7RAhzEDI8PSYhE41OmHsHg24YSoT/nXjeZW68Kvy1+cr8t7zrSKaocLpsE7lppEy KSdVo6Sr8LMw9bL7H3PU+CyRRwKm+SFZav7VpROzCf/kTil1aexdPrvtALqK2c4CYwiN 7xbQ==
MIME-Version 1.0
In-Reply-To <509B2355.60407@r3dsolutions.com>
References <mailman.3267.1352096879.27098.python-list@python.org> <50978323$0$6908$e4fe514c@news2.news.xs4all.nl> <CAHVvXxQR2J2tyFcQZd7bNYTAeetwM+LfoV4UcYrkRfeNVOj+pg@mail.gmail.com> <CAPTjJmqzQi2TEjme_we-KfmG7vxOfRWoKG=zL=SGHYGqiFf9_Q@mail.gmail.com> <CAHVvXxQNCxpACM=tkKir0f3iVqui4oVGnyS-GywO+dtGacKAdQ@mail.gmail.com> <mailman.3311.1352181444.27098.python-list@python.org> <5098d2ac$0$29980$c3e8da3$5496439d@news.astraweb.com> <mailman.3343.1352242047.27098.python-list@python.org> <5099bf7d$0$29980$c3e8da3$5496439d@news.astraweb.com> <509ADA6E.10403@r3dsolutions.com> <CALwzidmkGMH0qXCAa=24-=Nej+CJ0asDMFKZbFxuXfcpzC=emA@mail.gmail.com> <509B2355.60407@r3dsolutions.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Thu, 8 Nov 2012 00:09:35 -0700
Subject Re: Multi-dimensional list initialization
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.3422.1352358609.27098.python-list@python.org> (permalink)
Lines 97
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352358609 news.xs4all.nl 6873 [2001:888:2000:d::a6]:39753
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32933

Show key headers only | View raw


On Wed, Nov 7, 2012 at 8:13 PM, Andrew Robinson
<andrew3@r3dsolutions.com> wrote:
> OK, and is this a main use case?  (I'm not saying it isn't I'm asking.)

I have no idea what is a "main" use case.

> There is a special keyword which signals the new type of comprehension;  A
> normal comprehension would say eg: '[ foo for i in xrange ]'; but when the
> 'for i in' is reduced to a specific keyword such as 'ini' (instead of
> problematic 'in') the caching form of list comprehension would start.

FYI, the Python devs are not very fond of adding new keywords.  Any
time a new keyword is added, existing code that uses that word as a
name is broken.  'ini' is particularly bad, because 1) it's not a
word, and 2) it's the name of a common type of configuration file and
is probably frequently used as a variable name in relation to such
files.

> So, then, just like a comprehension -- the interpreter will begin to
> evaluate the code from the opening bracket '['; But anything other than a
> function/method will raise a type error (people might want to change that,
> but it's safe).
>
> The interpreter then caches all functions/initialiser methods it comes into
> contact with.
> Since every function/method has a parameter list (even if empty);  The
> interpreter would evaluate the parameter list on the first pass through the
> comprehension, and cache each parameter list with it's respective function.
>
> When the 'ini' keyword is parsed a second time, Python would then evaluate
> each cached function on its cached parameter list; and the result would be
> stored in the created list.
> This cached execution would be repeated as many times as is needed.
>
> Now, for your example:
>
> values = zip(samples, times * num_groups)
>     if len(values) < len(times) * num_groups:
>         # raise an error
>
> Might be done with:
>
> values = zip(   samples, [ lambda:times, ini xrange(num_groups) ]   )
>
>     if len(values) < len(times) * num_groups
>
> The comma after the lambda is questionable, and this construction would be
> slower since lambda automatically invokes the interpreter; but it's correct.

How is this any better than the ordinary list comprehension I already
suggested as a replacement?  For that matter, how is this any better
than list multiplication?  Your basic complaint about list
multiplication as I understand it is that the non-copying semantics
are unintuitive.  Well, the above is even less intuitive.  It is
excessively complicated and almost completely opaque.  If I were to
come across it outside the context of this thread, I would have no
idea what it is meant to be doing.

> As an aside, how would you do the lambda inside a list comprehension?

As a general rule, I wouldn't.  I would use map instead.

> [lambda:6 for i in xrange(10) ] # Nope.

Thak constructs a list of 10 functions and never calls them.  If you
want to actually call the lambda, then:

[(lambda: 6)() for i in range(10)]

or:

map(lambda i: 6, range(10))

But note that the former creates equivalent 10 functions and calls
each of them once, whereas the latter creates one function and calls
it ten times.

>> Of course you got an integer. You took an index of the range object, not a
>> slice. The rule is that taking an index of a sequence returns an element;
>> taking a slice of a sequence returns a sub-sequence. You still have not
>> shown any inconsistency here.
>
>
> Because it's an arbitrary rule which operates differently than the
> traditional idea shown in python docs?
>
> slice.indices()  is *for* (QUOTE)"representing the set of indices specified
> by range(start, stop, step)"
> http://docs.python.org/2/library/functions.html#slice

slice.indices() has nothing to do with it.  Indexing a sequence and
calling the .indices() method on a slice are entirely different
operations.  The slice.indices method is a utility method meant to be
called by __getitem__ implementations when doing slicing, not an
implementation of indexing.  When a sequence is indexed, there is no
slice.  That method is not related in any way to the semantics of
indexing a sequence.

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


Thread

Multi-dimensional list initialization Demian Brecht <demianbrecht@gmail.com> - 2012-11-04 22:27 -0800
  Re: Multi-dimensional list initialization Hans Mulder <hansmu@xs4all.nl> - 2012-11-05 10:13 +0100
    Re: Multi-dimensional list initialization Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-06 01:32 +0000
    Re: Multi-dimensional list initialization Chris Angelico <rosuav@gmail.com> - 2012-11-06 13:01 +1100
    Re: Multi-dimensional list initialization Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-06 02:30 +0000
    Re: Multi-dimensional list initialization Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-05 21:51 -0800
      Re: Multi-dimensional list initialization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-06 09:04 +0000
        RE: Multi-dimensional list initialization "Shambhu Rajak" <shambhu.rajak@calsoftinc.com> - 2012-11-06 18:57 +0530
        Re: Multi-dimensional list initialization Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-06 14:41 -0800
          Re: Multi-dimensional list initialization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-07 01:55 +0000
            Re: Multi-dimensional list initialization Demian Brecht <demianbrecht@gmail.com> - 2012-11-06 22:56 -0800
            Re: Multi-dimensional list initialization wxjmfauth@gmail.com - 2012-11-07 00:57 -0800
            Re: Multi-dimensional list initialization Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-07 22:27 +0000
            Re: Multi-dimensional list initialization Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-07 16:39 -0700
            Re: Multi-dimensional list initialization Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-08 00:09 -0700
            Re: Multi-dimensional list initialization Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-08 08:58 -0700
            Re: Multi-dimensional list initialization Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-09 01:39 +0000
            Re: Multi-dimensional list initialization Chris Angelico <rosuav@gmail.com> - 2012-11-09 17:07 +1100
              Re: Multi-dimensional list initialization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-09 06:37 +0000
                Re: Multi-dimensional list initialization Chris Angelico <rosuav@gmail.com> - 2012-11-09 17:59 +1100
                Re: Multi-dimensional list initialization Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-09 07:27 +0000
                Re: Multi-dimensional list initialization rusi <rustompmody@gmail.com> - 2012-11-09 07:05 -0800
                Re: Multi-dimensional list initialization Chris Angelico <rosuav@gmail.com> - 2012-11-10 02:23 +1100
            Re: Multi-dimensional list initialization Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-09 14:34 -0500
            RE: Multi-dimensional list initialization "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-09 20:31 +0000
            Re: Multi-dimensional list initialization Ethan Furman <ethan@stoneleaf.us> - 2012-11-09 13:49 -0800
        RE: Multi-dimensional list initialization "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-06 23:39 +0000
        Re: Multi-dimensional list initialization Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-06 16:52 -0700
        Re: Multi-dimensional list initialization MRAB <python@mrabarnett.plus.com> - 2012-11-07 00:23 +0000
          Re: Multi-dimensional list initialization rusi <rustompmody@gmail.com> - 2012-11-06 20:11 -0800
          Re: Multi-dimensional list initialization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-07 05:05 +0000
            Re: Multi-dimensional list initialization Roy Smith <roy@panix.com> - 2012-11-07 00:12 -0500
              Re: Multi-dimensional list initialization Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2012-11-07 18:32 +1300
                RE: Multi-dimensional list initialization "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-07 15:57 +0000
            Re: Multi-dimensional list initialization Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-11-07 10:52 +0200
            Re: Multi-dimensional list initialization MRAB <python@mrabarnett.plus.com> - 2012-11-07 17:17 +0000
              Re: Multi-dimensional list initialization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-07 22:36 +0000
        Re: Multi-dimensional list initialization Ethan Furman <ethan@stoneleaf.us> - 2012-11-07 07:23 -0800
        Re: Multi-dimensional list initialization Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-07 14:01 -0700
          Re: Multi-dimensional list initialization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-08 00:00 +0000
            Re: Multi-dimensional list initialization Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-08 00:30 +0000
              Re: Multi-dimensional list initialization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-08 03:47 +0000
            Re: Multi-dimensional list initialization Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-07 20:51 -0800
            Re: Multi-dimensional list initialization wrw@mac.com - 2012-11-08 08:26 -0500
        Re: Multi-dimensional list initialization Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-07 16:24 -0800
          Re: Multi-dimensional list initialization Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-08 04:20 +0000
    Re: Multi-dimensional list initialization Chris Angelico <rosuav@gmail.com> - 2012-11-06 17:07 +1100
    Re: Multi-dimensional list initialization Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-06 00:21 -0800
    Re: Multi-dimensional list initialization Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-06 02:19 -0700
    RE: Multi-dimensional list initialization "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-06 17:32 +0000
    Re: Multi-dimensional list initialization Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-06 13:14 -0800
    Re: Multi-dimensional list initialization Andrew Robinson <andrew3@r3dsolutions.com> - 2012-11-06 13:19 -0800
    Re: Multi-dimensional list initialization Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-06 15:46 -0700
      Re: Multi-dimensional list initialization Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2012-11-07 18:34 +1300
        Re: Multi-dimensional list initialization Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-07 14:00 +0000
        Re: Multi-dimensional list initialization Ethan Furman <ethan@stoneleaf.us> - 2012-11-07 06:47 -0800
        Re: Multi-dimensional list initialization Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-11-07 23:06 +0000
        Re: Multi-dimensional list initialization Greg Ewing <greg.ewing@canterbury.ac.nz> - 2012-11-08 14:29 +1300
  Re: Multi-dimensional list initialization wxjmfauth@gmail.com - 2012-11-05 01:55 -0800

csiph-web