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

Discussion in 'Python' started by Terry Jan Reedy, Apr 16, 2013.

  1. 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]
    Terry Jan Reedy, Apr 16, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Nogradi
    Replies:
    3
    Views:
    337
    Dennis Lee Bieber
    Nov 10, 2006
  2. aaB
    Replies:
    7
    Views:
    125
    Steven D'Aprano
    Apr 17, 2013
  3. Chris Angelico
    Replies:
    0
    Views:
    98
    Chris Angelico
    Apr 16, 2013
  4. Dave Angel
    Replies:
    0
    Views:
    86
    Dave Angel
    Apr 16, 2013
  5. Ethan Furman
    Replies:
    0
    Views:
    61
    Ethan Furman
    Apr 16, 2013
Loading...

Share This Page