a couple of things I don't understand wrt lists

Larry Hudson orgnut at yahoo.com
Wed Apr 17 06:57:06 CEST 2013


On 04/16/2013 08:37 AM, aaB wrote:
> hello,
>
<snip>
>
> 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
>

It appears what you are trying to do is create an eight-element list corresponding to the binary 
value of rulenum.  Here is a different approach, using some other functions and features of 
Python...

First you can use the bin() function to get the binary value of an integer as a string.  But 
this string has a leading '0b', which can be cut off with slicing.  eg. (using your example 
value of 8 and bs as the variable name for the binary string)

bin(8) gives the string '0b1000', so slicing off the two leading characters:

bs = bin(8)[2:]
bs becomes the string '1000'

Now, the number of leading zeros needed is (8 - len(bs)).  You can create the initial list of 
leading zeros with the statement:

rule = [0] * (8 - len(bs)).
This gives rule as the list [0, 0, 0, 0].

Finally use a for loop to append the rest if the binary string as integers
for i in bs:
     rule.append(int(i))

So your function becomes (with a few changed variable names):

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

This can be shortened even more with a list comprehension.  You can think of a list 
comprehension essentially as a terse variation of a for loop specifically to create a list.
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]

Again, I'm assuming you specifically want an 8-element list.  But this could easily be 
generalized for any length by adding a second function parameter, say size, and change the 8 in 
the last statement to size.

<snip>

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

Don't worry about the length -- my reply is plenty wordy as well.  I'm hoping I went into enough 
detail that you can understand it without it being too advanced.  Mainly I was just trying to 
show you a different approach using other features of Python.  There's nothing wrong with your 
approach, it is quite straight-forward.  In fact I used the same basic approach when I was 
writing a C function to commify the display of integers -- that is, to add commas every third 
digit.(*)  Just about anything in programming can be approached in several different ways.  I'm 
not saying mine is better, and certainly not the best, just different.  I'm not a newbie but FAR 
from an expert.  Keep learning Python, I think you'll like it.

(*)A special function for this isn't necessary in Python, it's already built in the the 
new-style string formatting.  Try this statement:  '{:,}'.format(1000000)
(That's a colon and comma inside the curly braces.)

      -=- Larry -=-




More information about the Python-list mailing list