Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'syntax': 0.04; 'elif': 0.05; 'filename:fname piece:py': 0.07; 'sys': 0.07; 'branching': 0.09; 'imported': 0.09; 'statements': 0.09; 'runs': 0.10; 'def': 0.12; 'random': 0.14; 'conditional': 0.16; 'expected,': 0.16; "function's": 0.16; 'prev,': 0.16; 'which,': 0.16; 'index': 0.16; 'bit': 0.19; 'module': 0.19; 'import': 0.22; 'previously': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'script.': 0.24; 'sorry,': 0.24; "haven't": 0.24; 'script': 0.25; 'least': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; "i'm": 0.30; 'code': 0.31; 'preliminary': 0.31; 'class': 0.32; 'linux': 0.33; 'running': 0.33; 'skip:# 10': 0.33; "can't": 0.35; 'subject:lists': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'machine.': 0.36; 'next': 0.36; 'shows': 0.36; 'charset:us-ascii': 0.36; 'list': 0.37; 'project': 0.37; 'system,': 0.38; 'message-id:@gmail.com': 0.38; 'stopped': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'that,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'lower': 0.61; 'content- disposition:inline': 0.62; 'show': 0.63; 'here': 0.66; 'behavior': 0.77; '257': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:date:from:to:subject:message-id:mail-followup-to :references:mime-version:content-type:content-disposition :in-reply-to:user-agent; bh=LgzgJ7dq8yuriCR53xbdimKsfIYFEqz8Xju9Ey4ngYQ=; b=q7cwoGjFHrVUNSQ4jV0T5+w/XhLS21gJ1iFr8qcnJOfsKpdXOSMZnDtj9Wy884yKSF IifI7Tlazj/hJBbZN9/gtSY86Zkhy0yLMr/sYuZqX7E/Mlit+4JgQVR57W0FBeaIlt2X zKlUKziT9KAPcdCC4tQIebxJuOwtqN47vgCxz5o06VM3W7LqWxnmG1g1miDtvPr1Xr9/ hDJ6jUyr35RvSPEi4MPqJLPXMG0SxTIkhM2WiR2YZ4uD4heCA98ibDfVpHtrU1FlEEiZ OZNWI8M6fs6vtctx5nJxUv+jqMYL5t0tBEp+zeUvLy8Fj7UYHyNmXfT+GJHv1GAzhL3g NHTg== X-Received: by 10.194.10.129 with SMTP id i1mr3012326wjb.21.1366290064000; Thu, 18 Apr 2013 06:01:04 -0700 (PDT) Date: Thu, 18 Apr 2013 15:01:01 +0200 From: aaB To: python-list@python.org Subject: Re: a couple of things I don't understand wrt lists Mail-Followup-To: python-list@python.org References: <20130416153701.GA18377@gmail.com> <20130417102537.GA19967@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="n8g4imXOkfNTN/H1" Content-Disposition: inline In-Reply-To: <20130417102537.GA19967@gmail.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 124 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366290071 news.xs4all.nl 2237 [2001:888:2000:d::a6]:53964 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43823 --n8g4imXOkfNTN/H1 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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: 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". The script is also attached to the email in case someone would like to try it on their machine. The print statements in the get_index() function's branching show me that, when I get the "out of range" error, the program doesn't enter the "elif i is n" condition, although it does for lower values of n. I'm sorry, but I still haven't found a way to copy/paste from the terminal to vim. This behaviour occurred previously and had stopped when I used an other version of the get_index() function. At that time, get_index() wasn't a function and was just conditional branching inside get_nextgen, I wanted to use the list comprehension syntax to build nextgen, and this got me into seperating the get_index() routine. --n8g4imXOkfNTN/H1 Content-Type: text/x-python; charset=us-ascii Content-Disposition: attachment; filename="testca.py" #!/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: 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) --n8g4imXOkfNTN/H1--