control direction of list

Calvin79 del at mjclift.freeserve.co.uk
Fri Aug 13 12:14:17 EDT 2004


Hi All,

I was given this in an earlier post by Satchit.


import random

things = xrange(int(raw_input("choose no of things (1-8)? ")))

state = [None,None]

l = [] 

for x in things:
    tmp = random.choice('abcd')
    print state
    while tmp in state[0:2]:
        tmp = random.choice('abcd')
    print "choice ",x+1," is ", tmp        
    l.append(tmp) 

state[x%2] = tmp

print 1

Could someone help in showing me how to control it like this? That if 'a'
is choosen it can only be followed by say b or d, if 'd' is choosen it can
only be followed by c or b etc... I can see how to do this after the list
has been generated with if statements, but that is impractical. As it is
at the moment it stop two of the same letters being given one after
another or the same letter occurring with only one space interviening.

Thanks,

Calvin




More information about the Python-list mailing list