Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: a couple of things I don't understand wrt lists

Reply
Thread Tools

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

 
 
Dave Angel
Guest
Posts: n/a
 
      04-16-2013
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()
>
> 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 "<stdin>", line 1, in <module>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a couple of things I don't understand wrt lists aaB Python 7 04-17-2013 06:46 PM
Re: a couple of things I don't understand wrt lists Terry Jan Reedy Python 0 04-16-2013 05:19 PM
Re: a couple of things I don't understand wrt lists Chris Angelico Python 0 04-16-2013 03:43 PM
Re: Lists of lists and tuples, and finding things within them Daniel Nogradi Python 3 11-10-2006 07:57 AM
Better way to do a couple things? Jon Bond HTML 2 08-08-2003 04:42 PM



Advertisments