[Tutor] weighted choices from among many lists

Kent Johnson kent37 at tds.net
Sat Mar 11 14:34:49 CET 2006

kevin parks wrote:
> I have several lists... and i would like to some times chose from one 
> list and for a while choose from a different list, etc.

You don't say what isn't working but I have a guess. The actual windex() 
function looks OK to me.

> def test():
> 	lst_a = [ 'red', 'green', 'blue', 'orange', 'violet', 'yellow', 
> 'black', 'white' ]
> 	lst_b = ['Rottweiler', 'Beagle', 'Sheepdog', 'Collie', 'Boxer', 
> 'Terrier', 'Bulldog', 'Chihuahua', 'Retriever', 'Collie', 'Dachshund', 
> 'Doberman', 'Greyhound', 'Pug', 'Spaniel']
> 	lst_c = ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']
> 	lst_d = ['fender', 'gibson', 'rickenbacker', 'guild', 'danelectro', 
> 'gretsch', 'martin', 'ibanez']
> 	x = [('lst_a', .50), ('lst_b', .25), ('lst_c', .10),('lst_d', .15)]

x is list containing the *names* of the other lists. You need to keep 
references to the lists so you can pick from them.
	x = [(lst_a, .50), (lst_b, .25), (lst_c, .10),(lst_d, .15)]

> 	i = 1
> 	while i < 100:
> 		lst = windex(x)
> 		print i, lst,

with the change above this will print the list, not its name
> 		pick = random.choice(lst)
but this will work.

If you want to be able to print the name of the list then you could 
include both the name and the actual list in x:

	x = [(('lst_a', lst_a), .50), etc...]


More information about the Tutor mailing list