[Tutor] First real script

Alan Gauld alan.gauld at btinternet.com
Mon Nov 6 19:11:04 CET 2006


"Carlos" <carloslara at web.de> wrote
> Here is the code:
> Note: Right now maya is not needed to run the code.
>
> import time
>
> #This is the initial state. You can put as many integers as you wish
> A_List = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
>
> A_Len = len(A_List)
> print 'A_List: ',A_List
> print 'A_Len: ', A_Len
> B_List = []
> S = A_Len
> print S

Not sure why you do this? Why not just use A_Len?

> #This is the number of iterations.
> for r in range (50):
>    for i in range (S):
>            #if i < S-1:
>                a = A_List[i-1]
>                b = A_List[i]
>                c = A_List[i+1]
>            else:
>                a = A_List[i-1]
>                b = A_List[i]
>                c = A_List[0]
>
>            if a == 1 and b == 1 and c == 1:
>                    X = 0
>            elif a == 1 and b == 1 and c == 0:
>                    X = 0
>            elif a == 1 and b == 0 and c == 1:
>                    X = 0

I would rewrite these tests to use tuples:

if (a,b,c) == (1,1,1): X = 0
elif (a,b,c) == (1,1,0): X = 0
elif (a,b,c) == (1,0,1): X = 0
etc...

You could also use a dictionary to make it sligtly more concise:

tests = {(1,1,1): 0, (1,1,0): 0,....(1,0,0): 1....}

then

X = tests[(a,b,c)]

I find either option easier to read and maintain than the
boolean expressions.

>            B_List.append(X)
>    print 'B_List: ',B_List
>    A_List = B_List
>    B_List = []

HTH,

Alan G. 




More information about the Tutor mailing list