a couple of things I don't understand wrt lists

Terry Jan Reedy tjreedy at udel.edu
Tue Apr 16 13:19:55 EDT 2013


On 4/16/2013 11:37 AM, aaB wrote:

> 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

divmod(rulenum) will return both the quotient and remainder at one time

>    while len(rule) < 8:
>      rule.append(0)
>    rule.reverse()
>    return rule

In versions of Python with builtin bin(), you could write

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

To know Python decently well, you should understand all of that syntax.

> rule = getrule(int(8))
> print rule
> [0, 0, 0, 0, 1, 0, 0, 0]





More information about the Python-list mailing list