Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'value,': 0.04; 'argument': 0.05; 'beginner': 0.05; 'output': 0.05; '[0,': 0.09; 'python': 0.11; 'def': 0.12; 'project,': 0.12; 'wrote': 0.14; '2.7': 0.14; 'expected,': 0.16; 'get,': 0.16; 'helps.': 0.16; 'integers,': 0.16; 'png': 0.16; 'received:74.208.4.195': 0.16; 'scripts.': 0.16; 'subscript.': 0.16; 'typeerror:': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; '>>>': 0.22; 'programming': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'script': 0.25; 'shown': 0.26; 'header:In-Reply- To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'am,': 0.29; 'errors': 0.30; 'list:': 0.30; 'code': 0.31; '"",': 0.31; 'preliminary': 0.31; 'file': 0.32; 'linux': 0.33; '(most': 0.33; "can't": 0.35; 'something': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'doing': 0.36; "didn't": 0.36; 'should': 0.36; 'half': 0.37; 'list': 0.37; 'list.': 0.37; 'represent': 0.38; 'skip:[ 10': 0.38; 'whatever': 0.38; 'to:addr :python-list': 0.38; 'list,': 0.38; 'recent': 0.39; 'expect': 0.39; 'to:addr:python.org': 0.39; 'system.': 0.39; 'how': 0.40; 'is.': 0.60; 'hope': 0.61; 'ago,': 0.61; 'length': 0.61; "you've": 0.63; 'more': 0.64; 'here': 0.66; 'received:74.208': 0.68; 'statement,': 0.68; 'results': 0.69; 'cellular': 0.84; 'console,': 0.84; 'good:': 0.84; 'retype': 0.84; 'do:': 0.91; 'same,': 0.91 Date: Tue, 16 Apr 2013 16:48:20 -0400 From: Dave Angel 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> In-Reply-To: <20130416153701.GA18377@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:lWiaoKAUSL5+/ZjrmwB7qDU8skOubzOdsxupilQ4YnF 31Cg0JJs69QglTcV8wkBDfrqbuUHmVCOk0/pvr8mdQUSY+0wKn pjuZcyAqxnURIviHHMbQR35gNsH9U6F/qSCg7kTVYeRS35nzPb YiwzWN57xxAABQldYedKKVtS6vckVGPaHCJL79LeiKAKIiq+BM yTs9KSQg/CQfFR9WDbNO7lUKTJZ1zraasr2MavEEpnnHlbEa77 JK2UDnlB0KgYACMg4k3fSr96oCiAajC6JMwMCtfxmGxusHfwF8 VTKcC22VFgdEsdvvh6MGNme1sMrMVohz12z31y98Dj5An3pNQ= = 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: 142 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366145320 news.xs4all.nl 2608 [2001:888:2000:d::a6]:37221 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43704 On 04/16/2013 11:37 AM, aaB wrote: > hello, > > I am a beginner programmer. I started learning programming about a year and a > half ago, using C. I picked up python a few months ago, but only wrote very few > scripts. > > I am currently trying to learn more about the python way of doing things by > writing a script that generates png images using a 1D cellular automaton. > > While writing preliminary code for that project, I ran into a behaviour that I > don't understand. > I am using python 2.7 on a linux system. > > I represent the CA's rule with a list of integers, of value 1 or 0. > Here is the function I use to generate the list: > > def get_rule(rulenum): > rule = [] > while rulenum > 0: > rule.append(rulenume % 2) > rulenum /= 2 > while len(rule) < 8: > rule.append(0) > rule.reverse() > return rule > > if i call it by writing: > > rule = getrule(int(8)) > > and then call: > > print rule > > the output is good: > > [0, 0, 0, 0, 1, 0, 0, 0] > > > I then tried to print each item of the list using a for loop: > There are copy/paste errors in your following pieces. Did you retype them instead of using the clipboard? > for i in range(rule): > print rule[i] > > the output is, as expected: > 0 > 0 > 0 > 0 > 1 > 0 > 0 > 0 > Here's what I get, and how I fix it: >>> rule = [0,0,0,0,1,0,0,0] >>> for i in range(rule): ... print i ... Traceback (most recent call last): File "", line 1, in TypeError: range() integer end argument expected, got list. >>> for i in range(len(rule)): ... print rule[i] ... 0 0 0 0 1 0 0 0 > but when I do: > > for i in rule: > print rule[i] You should be printing i here, not rule[i] > > I get the "complement": > 1 > 1 > 1 > 1 > 0 > 1 > 1 > 1 I don't. And don't expect to. It's nothing like the complement. >>> for i in rule: ... print rule[i] ... 0 0 0 0 0 0 0 0 Anyway, to fix it, just print the value, don't try to use it as a subscript. >>> for value in rule: ... print value ... 0 0 0 0 1 0 0 0 > > There must be something I didn't understand correctly in the for statement, but > I really can't think of a reason why the output is what it is. > I tried this using the interactive console, and the results are the same, > whatever the length of the list, i always get the complement of my bit pattern. You should never get the complement of the bit pattern with any code you've shown above. > Hope that helps. -- DaveA