Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43689 > unrolled thread

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

Started byTerry Jan Reedy <tjreedy@udel.edu>
First post2013-04-16 13:19 -0400
Last post2013-04-16 13:19 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: a couple of things I don't understand wrt lists Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-16 13:19 -0400

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

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-04-16 13:19 -0400
SubjectRe: a couple of things I don't understand wrt lists
Message-ID<mailman.678.1366132807.3114.python-list@python.org>
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]

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web