Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43833
| Return-Path | <rosuav@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; 'else:': 0.03; 'skip:[ 20': 0.04; 'value,': 0.04; 'cpython': 0.05; 'elif': 0.05; 'say,': 0.05; 'debugging': 0.07; 'happens.': 0.09; 'integers': 0.09; 'matched': 0.09; 'skip:/ 10': 0.09; 'subset': 0.09; 'sure,': 0.09; 'def': 0.12; '"=="': 0.16; '(without': 0.16; 'caches': 0.16; 'clause,': 0.16; 'equality.': 0.16; 'fire,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'identity:': 0.16; 'integers,': 0.16; 'opposite': 0.16; 'prev,': 0.16; 'set,': 0.16; 'ways:': 0.16; '(you': 0.16; 'exception': 0.16; 'index': 0.16; 'wrote:': 0.18; 'result.': 0.19; 'thu,': 0.19; 'print': 0.22; 'case.': 0.24; 'source': 0.25; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; 'testing': 0.29; 'possibility': 0.29; "doesn't": 0.30; 'message- id:@mail.gmail.com': 0.30; 'work.': 0.31; 'equality': 0.31; 'option': 0.32; 'could': 0.34; "can't": 0.35; 'subject:lists': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'sequence': 0.36; 'done': 0.36; 'doing': 0.36; 'next': 0.36; 'two': 0.37; 'list': 0.37; 'sometimes': 0.38; 'depends': 0.38; 'version,': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'either': 0.39; 'even': 0.60; 'dangerous': 0.60; 'most': 0.60; 'impact': 0.61; "you're": 0.61; 'day.': 0.63; 'within': 0.65; 'default': 0.69; 'therefore': 0.72; 'to,': 0.72; 'day': 0.76; 'lack': 0.78; 'low': 0.83; 'here)': 0.84; 'fun!': 0.93; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=NwjlQEuWqd1MO0x7VBrysZMmbWAZEJ/hCPoVKlpZzpg=; b=xmT8u6DH+ce6rKx+z2yyR8zdluT3Lq6VgBxl2I21cCzx/FuVoPjJr6ofWyuXsVSQre DzRzp7/KYeqlxTqBtHfYv0p8rxvDygdvvXgYLBu1lWYN1uc1uiFcSLFjo0SXspL0IL7v kMDyCemKZL/CsxoGfMjq7QydOrYEcCKlfHnfM4dcFkJXB4MmiTV3Zd+8qsblfRAoCX/b vKDn/55n8IgN73/H4Qvacx8RNULm1uoMobDeaCQbduaWhFx60kKpheKbpPg+g1pE/rbH Bm0HigC6J60Y8IeIxkVkF1ktZNpCBZV1NvShr+/HEbF6qIeNX1uWQDzYKrAa1cr6apnU GouQ== |
| MIME-Version | 1.0 |
| X-Received | by 10.52.91.230 with SMTP id ch6mr6933446vdb.42.1366295234822; Thu, 18 Apr 2013 07:27:14 -0700 (PDT) |
| In-Reply-To | <20130418130101.GA22185@gmail.com> |
| References | <20130416153701.GA18377@gmail.com> <CAPTjJmqRB0eiG9peDOU+Ow_1rqq2u=xXcUduyxUm3gAhrzMjHg@mail.gmail.com> <20130417102537.GA19967@gmail.com> <20130418130101.GA22185@gmail.com> |
| Date | Fri, 19 Apr 2013 00:27:14 +1000 |
| Subject | Re: a couple of things I don't understand wrt lists |
| From | Chris Angelico <rosuav@gmail.com> |
| To | 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.772.1366295237.3114.python-list@python.org> (permalink) |
| Lines | 67 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1366295237 news.xs4all.nl 2232 [2001:888:2000:d::a6]:58949 |
| X-Complaints-To | abuse@xs4all.nl |
| Path | csiph.com!usenet.pasdenom.info!news.stben.net!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
| Xref | csiph.com comp.lang.python:43833 |
Show key headers only | View raw
On Thu, Apr 18, 2013 at 11:01 PM, aaB <mecagonoisician@gmail.com> wrote: > def get_index(thisgen, i): > n = len(thisgen)-1 > cell = thisgen[i] > if i is 0: > print "i==0" > prev, next = thisgen[n], thisgen[i+1] > elif i is n: > print "i==%d" % n > prev, next = thisgen[i-1], thisgen[0] > else: > prev, next = thisgen[i-1], thisgen[i+1] > return prev*4 + cell*2 + next Without seeing the exception traceback I can't be sure, but here's what I think might be happening: When n == 258, your "i is n" check never happens. Since your protective check for the end of the list doesn't fire, you therefore go into the 'else' clause, and attempt to index thisgen[i+1], which doesn't work. CPython precreates and caches a certain subset of integers, for performance. The exact set depends on the CPython version, but it's low integers only. Within that set, equality is always matched by identity: i = 3 j = i+1 k = j-1 print(i is k) This will most likely print "True" on CPython. But change i to, say, 10000000, and you may find the opposite result. So you can sometimes get away with the dangerous option of testing integers for identity, but the right thing to do is to test for equality. (You even get that right in your debugging messages, using "==" there.) A couple of other tips: > n = len(thisgen)-1 > if i is 0: > prev, next = thisgen[n], thisgen[i+1] You can reference thisgen[-1] instead of thisgen[n] - so you can fold this one into the default case. So all you need to do is deal with the one possibility that i==len(thisgen)-1 and everything else is in the else. > def populate(n): > random.seed() > return [random.randint(0,1) for i in range(n)] Don't reseed your RNG every call; just seed it once and let it run. Seeding the RNG (without using a specific value, which you're not doing here) is done in one of two ways: either from an OS-supplied source of randomness (eg /dev/random), or from the time of day. Reseeding repeatedly from /dev/random is unnecessary and might impact other processes (forcing them to block for lack of available entropy); reseeding from the time of day could mean that every call to populate() returns the exact same sequence of numbers. Have fun! ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: a couple of things I don't understand wrt lists Chris Angelico <rosuav@gmail.com> - 2013-04-19 00:27 +1000
csiph-web