[Tutor] weighted choices from among many lists

kevin parks kp8 at mac.com
Sun Mar 12 02:06:47 CET 2006


On Mar 11, 2006, at 3:24 PM, tutor-request at python.org wrote:

>
> Message: 1
> Date: Sat, 11 Mar 2006 08:34:49 -0500
> From: Kent Johnson <kent37 at tds.net>
> Subject: Re: [Tutor] weighted choices from among many lists
> Cc: tutor at python.org
> Message-ID: <4412D1F9.3040906 at tds.net>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> 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.

yes, that part always worked fine... not the most robust thing, but it 
works.


>
>> 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 am an idiot... someone shoot me.. i guess i got so carried away with 
typing
the single quotes for the above lists that when i went to pack those up 
for windex
i didn't realize that i had needlessly typed 'lst_a', etc. Sometimes 
you just need a
fresh pair of eyes to pick up on that type of thing....


>> 	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...]


That produces:
('lst_c', ['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout'])
['Ale', 'Lager', 'Pilsner', 'Bock', 'Porter', 'Stout']

i am actually trying to get name of list, the choice like so:

lst_c
Bock



More information about the Tutor mailing list