Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43689
| From | Terry Jan Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: a couple of things I don't understand wrt lists |
| Date | 2013-04-16 13:19 -0400 |
| References | <20130416153701.GA18377@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.678.1366132807.3114.python-list@python.org> (permalink) |
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]
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: a couple of things I don't understand wrt lists Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-16 13:19 -0400
csiph-web