Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43675
| Date | 2013-04-16 17:37 +0200 |
|---|---|
| From | aaB <mecagonoisician@gmail.com> |
| Subject | a couple of things I don't understand wrt lists |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.668.1366126626.3114.python-list@python.org> (permalink) |
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:
for i in range(rule):
print rule[i]
the output is, as expected:
0
0
0
0
1
0
0
0
but when I do:
for i in rule:
print rule[i]
I get the "complement":
1
1
1
1
0
1
1
1
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.
Any pointers to help me understand this would be appreciated.
I am also running into an other issue with this script, but I'd rather
understand this before asking further questions.
Thanks, and sorry for the rather long post.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
a couple of things I don't understand wrt lists aaB <mecagonoisician@gmail.com> - 2013-04-16 17:37 +0200
Re: a couple of things I don't understand wrt lists John Gordon <gordon@panix.com> - 2013-04-16 16:43 +0000
Re: a couple of things I don't understand wrt lists Larry Hudson <orgnut@yahoo.com> - 2013-04-16 21:57 -0700
Re: a couple of things I don't understand wrt lists Serhiy Storchaka <storchaka@gmail.com> - 2013-04-17 12:35 +0300
Re: a couple of things I don't understand wrt lists 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-17 09:10 -0700
Re: a couple of things I don't understand wrt lists Ned Batchelder <ned@nedbatchelder.com> - 2013-04-17 14:36 -0400
Re: a couple of things I don't understand wrt lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-18 04:46 +1000
Re: a couple of things I don't understand wrt lists Chris Angelico <rosuav@gmail.com> - 2013-04-18 04:42 +1000
Re: a couple of things I don't understand wrt lists 88888 Dihedral <dihedral88888@googlemail.com> - 2013-04-17 09:10 -0700
csiph-web