set & random.choice question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stevecanfield@yahoo.com

    #1

    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

  • Lawrence Oluyede

    #2
    Re: set & random.choice question

    Il 2005-12-14, stevecanfield@y ahoo.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(l ist(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"

    Comment

    • Christophe Delord

      #3
      Re: set &amp; random.choice question

      Hello,

      On 14 Dec 2005 12:16:22 -0800, stevecanfield@y ahoo.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(l ist(x)) ?


      Christophe.

      Comment

      • Dennis Benzinger

        #4
        Re: set &amp; random.choice question

        stevecanfield@y ahoo.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

        Comment

        • cgriddell@gmail.com

          #5
          Re: set &amp; random.choice question

          Oh duh. :)

          Thanks for pointing out the obvious without mocking...

          sc

          Comment

          Working...