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


Groups > comp.lang.python > #78070

Re: Is there a canonical way to check whether an iterable is ordered?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'true,': 0.05; 'allowed.': 0.07; 'false,': 0.09; 'subject:iterable': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'random': 0.14; 'dict': 0.16; 'expression.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'items(self):': 0.16; 'keys(self):': 0.16; 'pairs': 0.16; 'pairs,': 0.16; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'possibly': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'sep': 0.31; 'steven': 0.31; 'class': 0.32; 'fri,': 0.33; 'skip:_ 10': 0.34; "can't": 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'list': 0.37; 'whatever': 0.38; 'pm,': 0.38; 'generating': 0.39; 'offer': 0.62; 'subject:there': 0.68; 'guarantee.': 0.84; 'subject:check': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=1a/Emu6+6Qifvtl9QCaCBo8lbQYstMPyC6XuA3fLAts=; b=MlTz5zxZaUrHyqBNy0r/rDsF/qrTPX2yDKueL70mQF0I4YM2Pty9RkUQhP27IJdWfD IcKTm4nfY2jX0ilN1O9qUq8F9SngfrXkBiqsFgL1NlcKaiezwrRUZipGwW6qKR4OImdV FHPKoaHPGDMUl0VJ2VJwJ4rF4Ve+2DZugTMylOaE8eBhZaj4cYCnMAK+Ha8q7WqjxWdJ fol5vkFvhoNa/8vygN+WL8XAIqKy0f2z2Jmt5nn97LnZ8Oz7kPUAePpM1FW9LolxcBGl KPADIFO8jIkYLeSblUnZ6xp6C7napnyfwDII4crHSCRz1IxcvR+QWjkLeA478/vZa0Y9 DwOw==
MIME-Version 1.0
X-Received by 10.50.176.202 with SMTP id ck10mr55129873igc.2.1411125904933; Fri, 19 Sep 2014 04:25:04 -0700 (PDT)
In-Reply-To <541c0c7d$0$29992$c3e8da3$5496439d@news.astraweb.com>
References <efcc61e6-f132-4f14-80b5-0536816b6c7b@googlegroups.com> <mailman.14101.1411042251.18130.python-list@python.org> <roy-E21095.08580518092014@news.panix.com> <541bbbe6$0$29982$c3e8da3$5496439d@news.astraweb.com> <mailman.14134.1411105214.18130.python-list@python.org> <541c0c7d$0$29992$c3e8da3$5496439d@news.astraweb.com>
Date Fri, 19 Sep 2014 21:25:04 +1000
Subject Re: Is there a canonical way to check whether an iterable is ordered?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14141.1411125907.18130.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1411125907 news.xs4all.nl 2939 [2001:888:2000:d::a6]:40183
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:78070

Show key headers only | View raw


On Fri, Sep 19, 2014 at 8:59 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Here's a proof of concept of what would be allowed:
>
> import random
> class MyDict:
>     def __init__(self, items):
>         self._items = list(dict(items).items())
>         self._flags = [False, False, False]
>     def keys(self):
>         k = [item[0] for item in self._items]
>         self._check(0)
>         return k
>     def values(self):
>         k = [item[1] for item in self._items]
>         self._check(1)
>         return k
>     def items(self):
>         k = self._items[:]
>         self._check(2)
>         return k
>     def _check(self, i):
>         self._flags[i] = True
>         if self._flags == [True, True, True]:
>             random.shuffle(self._items)
>             self._flags = [False, False, False]

Also, this can't possibly offer the same guarantee. Watch:

d = MyDict(some_lot_of_items)
d.values(); d.items()
# mutate the dict in whatever way you like
pairs = zip(d.keys(), d.values())

This might well create mismatched pairs, because after generating the
keys() return value, the list gets shuffled, prior to generating
values() in the same expression. This would not be allowed.

ChrisA

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


Thread

Is there a canonical way to check whether an iterable is ordered? cool-RR <ram.rachum@gmail.com> - 2014-09-18 04:55 -0700
  Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-18 22:10 +1000
    Re: Is there a canonical way to check whether an iterable is ordered? Roy Smith <roy@panix.com> - 2014-09-18 08:58 -0400
      Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-18 23:33 +1000
        Re: Is there a canonical way to check whether an iterable is ordered? Roy Smith <roy@panix.com> - 2014-09-18 19:52 -0400
          Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 12:45 +1000
          Re: Is there a canonical way to check whether an iterable is ordered? Terry Reedy <tjreedy@udel.edu> - 2014-09-19 18:02 -0400
          Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-20 15:01 +1000
      Re: Is there a canonical way to check whether an iterable is ordered? Terry Reedy <tjreedy@udel.edu> - 2014-09-18 09:46 -0400
      Re: Is there a canonical way to check whether an iterable is ordered? Tim Chase <python.list@tim.thechases.com> - 2014-09-18 09:32 -0500
      Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 15:15 +1000
        Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 15:40 +1000
          Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 20:59 +1000
            Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 21:19 +1000
              Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 21:58 +1000
                Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 22:06 +1000
            Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 21:25 +1000
              Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 21:46 +1000
                Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 21:56 +1000
                Re: Is there a canonical way to check whether an iterable is ordered? alister <alister.nospam.ware@ntlworld.com> - 2014-09-19 12:26 +0000
                Re: Is there a canonical way to check whether an iterable is ordered? Chris Angelico <rosuav@gmail.com> - 2014-09-19 22:36 +1000
  Re: Is there a canonical way to check whether an iterable is ordered? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-19 15:04 +1000

csiph-web