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


Groups > comp.lang.python > #43824

Re: a couple of things I don't understand wrt lists

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <davea@davea.name>
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; 'elif': 0.05; 'sys': 0.07; 'assuming': 0.09; 'bits': 0.09; 'encode': 0.09; 'exception.': 0.09; 'imported': 0.09; 'runs': 0.10; 'def': 0.12; 'random': 0.14; '"is"': 0.16; 'bugs.': 0.16; 'elf': 0.16; 'expected,': 0.16; 'larger,': 0.16; 'prev,': 0.16; 'received:74.208.4.195': 0.16; 'spurious': 0.16; 'traceback.': 0.16; 'which,': 0.16; 'exception': 0.16; 'index': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'value.': 0.19; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'script.': 0.24; 'visible': 0.24; 'script': 0.25; 'compare': 0.26; 'second': 0.26; 'post': 0.26; 'least': 0.26; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'matching': 0.30; 'mode': 0.30; '(which': 0.31; 'code': 0.31; 'getting': 0.31; 'indentation': 0.31; 'preliminary': 0.31; 'class': 0.32; 'text': 0.33; 'linux': 0.33; 'everyone': 0.33; 'running': 0.33; 'guess': 0.33; 'skip:# 10': 0.33; "i'd": 0.34; "can't": 0.35; 'no,': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'object,': 0.36; 'next': 0.36; 'shows': 0.36; 'should': 0.36; 'list': 0.37; 'project': 0.37; 'list.': 0.37; 'expected': 0.38; 'system,': 0.38; 'to:addr:python- list': 0.38; 'issue': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'mailing': 0.39; "you're": 0.61; 'making': 0.63; 'happen': 0.63; 'more': 0.64; 'here': 0.66; 'received:74.208': 0.68; 'safe': 0.72; 'behavior': 0.77; '257': 0.84; 'look.': 0.84
Date Thu, 18 Apr 2013 09:23:22 -0400
From Dave Angel <davea@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4
MIME-Version 1.0
To python-list@python.org
Subject Re: a couple of things I don't understand wrt lists
References <20130416153701.GA18377@gmail.com> <CAPTjJmqRB0eiG9peDOU+Ow_1rqq2u=xXcUduyxUm3gAhrzMjHg@mail.gmail.com> <20130417102537.GA19967@gmail.com> <20130418130101.GA22185@gmail.com>
In-Reply-To <20130418130101.GA22185@gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:B39MiEJBq/EBst333MdGxaxRXAQPAdaJzwWlrIRCUXg Ibb97ihUstcLAIikRigptU707cMtaRUHp4w/ROaVIcGYZ0h8sX DRAK1lPneFdhHll2MUguGzQiUPmdZPNz/rl/eKtYvUgRTTu2xY 9zdNvLjOLgAM8FKPTv1o7ttemFVIXJYQ6lt4hWqI6Uk1yMkCm1 Ad8ppnB9f6uwx7JcG0LjpWlr6H+6WhLUe9gy63aQC+HUL45mnN sNBzsxjwQBJEQ842C6tagXWxWFjewMcs66ifV3f6kQxhVwVlgt SONH3btaC85jlWGsDOfUqSLnA81RdDaX73XadSJrjyLa33AzQ= =
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.767.1366291424.3114.python-list@python.org> (permalink)
Lines 89
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366291425 news.xs4all.nl 2250 [2001:888:2000:d::a6]:33057
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43824

Show key headers only | View raw


On 04/18/2013 09:01 AM, aaB wrote:
> Hello,
>
> I am still in the process of writing preliminary code for my CA project.
> I am now running into a behavior that I can't explain.
>
> Here is a script which, at least on my system, shows the issue (python2.7 on a
> linux system).
> The final project will be wrapping these functions (and others) into a class
> which will be in a module which can be imported into a script.
>
> #!/usr/bin/python2
>
> import sys
> import random
>
> def get_rule(rulenum):
>    bitpattern = bin(rulenum)[2:]
>    return [0]*(8-len(bitpattern)) + [int(bit) for bit in bitpattern]
>
> def populate(n):
>    random.seed()
>    return [random.randint(0,1) for i in range(n)]
>
> 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:

Don't use 'is' here and above.  You're not looking for a matching 
object, you're looking for the same value.

>      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
>
> def get_nextgen(thisgen, rule):
>    return [rule[get_index(thisgen, i)] for i in range(len(thisgen))]
>
>
> if len(sys.argv) == 2:
>    n = int(sys.argv[1])
> else:
>    n = 257
>
> rule = get_rule(145)
> thisgen = populate(n)
> nextgen = get_nextgen(thisgen, rule)
> print "done for n == 257"
>
> n = 258
> thisgen = populate(n)
> nextgen = get_nextgen(thisgen, rule)
>
>
> My issue is that when n == 257, the script runs as expected, but if n >= 258, I
> get an "IndexError: list index out of range".

No, you get an exception traceback.  Please paste in the whole traceback 
instead of making everyone guess where in the code it might be getting 
the exception.

My *guess* is that somewhere you're assuming that 8 bits is enough to 
encode 258 possibilities.  I'd have expected that to fail at 256 and 
larger, but it's a place to look.

The second guess, more likely, is that you're using "is" to compare 
numbers, and that's never a safe idea.  It might happen to work for 
small numbers, but you should be using ==.

    if i == 0:
    elf i == n:


> The script is also attached to the email

Attachments aren't necessarily visible to everyone reading the mailing 
list.  Just make sure you post in text mode (which you did), and that 
should take care of spurious indentation bugs.



-- 
DaveA

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


Thread

Re: a couple of things I don't understand wrt lists Dave Angel <davea@davea.name> - 2013-04-18 09:23 -0400

csiph-web