a couple of things I don't understand wrt lists

A

aaB

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

the output is, as expected:
0
0
0
0
1
0
0
0

but when I do:

for i in rule:
print rule

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.
 
J

John Gordon

In said:
but when I do:
for i in rule:
print rule

I get the "complement":
1
1
1
1
0
1
1
1

When you iterate over a list with this statement:

for i in rule:

i contains each successive list item.

However, your code:

print rule

acts as though i is the list item's *subscript*, which is incorrect.
In effect, your code is doing this:
print rule[0]
print rule[0]
print rule[0]
print rule[0]
print rule[1]
print rule[0]
print rule[0]
print rule[0]

Although in your example both rule[0] and rule[1] are zero, so I
don't know why '1' ever got printed.

Also, as far as I can tell, this code should not have worked at all:

for i in range(rule):
print rule

The range() function expects an integer, not a list.
 
S

Serhiy Storchaka

17.04.13 07:57, Larry Hudson напиÑав(ла):
So using a list comprehension you can do it in two lines:

def get_rule(num):
bs = bin(num)[2:]
return [0] * (8 - len(bs)) + [int(i) for i in bs]

You can do it in one line!

def get_rule(num):
return list(map(int, '{:08b}'.format(num)))
 
8

88888 Dihedral

Serhiy Storchakaæ–¼ 2013å¹´4月17日星期三UTC+8下åˆ5時35分07秒寫é“:
17.04.13 07:57, Larry Hudson напиÑав(ла):
So using a list comprehension you can do it in two lines:

def get_rule(num):
bs = bin(num)[2:]
return [0] * (8 - len(bs)) + [int(i) for i in bs]



You can do it in one line!



def get_rule(num):

return list(map(int, '{:08b}'.format(num)))

Well, a new object is returned and can be used.
Then who is going to clean up the object when required?
 
8

88888 Dihedral

Serhiy Storchakaæ–¼ 2013å¹´4月17日星期三UTC+8下åˆ5時35分07秒寫é“:
17.04.13 07:57, Larry Hudson напиÑав(ла):
So using a list comprehension you can do it in two lines:

def get_rule(num):
bs = bin(num)[2:]
return [0] * (8 - len(bs)) + [int(i) for i in bs]



You can do it in one line!



def get_rule(num):

return list(map(int, '{:08b}'.format(num)))

Well, a new object is returned and can be used.
Then who is going to clean up the object when required?
 
N

Ned Batchelder

Serhiy Storchakaæ–¼ 2013å¹´4月17日星期三UTC+8下åˆ5時35分07秒寫é“:
17.04.13 07:57, Larry Hudson напиÑав(ла):
So using a list comprehension you can do it in two lines:
def get_rule(num):
bs = bin(num)[2:]
return [0] * (8 - len(bs)) + [int(i) for i in bs]


You can do it in one line!



def get_rule(num):

return list(map(int, '{:08b}'.format(num)))
Well, a new object is returned and can be used.
Then who is going to clean up the object when required?

This is a key thing to understand about Python: memory is managed
automatically, no one has to clean up the object. Once there are no
names referring to the object, it will be cleaned up automatically.

--Ned.
 
C

Chris Angelico

This is a key thing to understand about Python: memory is managed
automatically, no one has to clean up the object. Once there are no names
referring to the object, it will be cleaned up automatically.

Dihedral is a bot. An amusing one, at times, but not someone you need
overly concern yourself with. :)

ChrisA
 
S

Steven D'Aprano

Ned said:
On 4/17/2013 12:10 PM, 88888 Dihedral wrote:

This is a key thing to understand about Python: memory is managed
automatically, no one has to clean up the object. Once there are no
names referring to the object, it will be cleaned up automatically.

The key to understand about 88888 Dihedral is that it is a bot.

:)

But seriously, it's a bot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top