set & random.choice question

Discussion in 'Python' started by stevecanfield@yahoo.com, Dec 14, 2005.

  1. Guest

    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
     
    , Dec 14, 2005
    #1
    1. Advertising

  2. Il 2005-12-14, <> ha scritto:
    > I want to do something like this:
    >
    > from random import choice
    > x = set(("jenny", "jacqui", "claire", "chris", "tracy"))
    > somebody = random.choice(x)


    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"
     
    Lawrence Oluyede, Dec 14, 2005
    #2
    1. Advertising

  3. Hello,

    On 14 Dec 2005 12:16:22 -0800, wrote:

    > 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?


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


    Christophe.
     
    Christophe Delord, Dec 14, 2005
    #3
  4. schrieb:
    > 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
    >


    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
     
    Dennis Benzinger, Dec 14, 2005
    #4
  5. Guest

    Oh duh. :)

    Thanks for pointing out the obvious without mocking...

    sc
     
    , Dec 15, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mickey Segal
    Replies:
    0
    Views:
    923
    Mickey Segal
    Feb 2, 2004
  2. Brian
    Replies:
    3
    Views:
    309
    Brian
    Sep 25, 2004
  3. miles.jg
    Replies:
    16
    Views:
    909
    Alf P. Steinbach
    Nov 14, 2007
  4. globalrev
    Replies:
    4
    Views:
    796
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,279
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page