Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 14th, 2005, 08:25 PM
stevecanfield@yahoo.com
Guest
 
Posts: n/a
Default set & random.choice question

I want to do something like this:

from random import choice
x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
somebody = random.choice(x)

but I bet a "TypeError: unindexable object" error. Any suggestions for
an elegant workaround?

I'm using set because I want to know that I have a collection of unique
objects.

steve

  #2  
Old December 14th, 2005, 08:45 PM
Lawrence Oluyede
Guest
 
Posts: n/a
Default Re: set & random.choice question

Il 2005-12-14, stevecanfield@yahoo.com <stevecanfield@yahoo.com> ha scritto:[color=blue]
> I want to do something like this:
>
> from random import choice
> x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
> somebody = random.choice(x)[/color]

import random
x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
somebody = random.choice(list(x))

You must turn back it into a list, set has no notion of indexing

--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
  #3  
Old December 14th, 2005, 08:45 PM
Christophe Delord
Guest
 
Posts: n/a
Default Re: set & random.choice question

Hello,

On 14 Dec 2005 12:16:22 -0800, stevecanfield@yahoo.com wrote:
[color=blue]
> I want to do something like this:
>
> from random import choice
> x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
> somebody = random.choice(x)
>
> but I bet a "TypeError: unindexable object" error. Any suggestions for
> an elegant workaround?[/color]

What about somebody = random.choice(list(x)) ?


Christophe.
  #4  
Old December 14th, 2005, 09:25 PM
Dennis Benzinger
Guest
 
Posts: n/a
Default Re: set & random.choice question

stevecanfield@yahoo.com schrieb:[color=blue]
> I want to do something like this:
>
> from random import choice
> x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
> somebody = random.choice(x)
>
> but I bet a "TypeError: unindexable object" error. Any suggestions for
> an elegant workaround?
>
> I'm using set because I want to know that I have a collection of unique
> objects.
>
> steve
>[/color]

import random

x = set(("jenny", "jacqui", "claire", "chris", "tracy"))


def draw_from_set(a_set):
random_index = random.randint(0, len(x) - 1)

for i, name in enumerate(x):
if i == random_index:
return name

somebody = draw_from_set(x)

print somebody


Bye,
Dennis
  #5  
Old December 15th, 2005, 07:05 AM
cgriddell@gmail.com
Guest
 
Posts: n/a
Default Re: set & random.choice question

Oh duh. :)

Thanks for pointing out the obvious without mocking...

sc

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles