select random entry from dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tor Erik Sønvisen

    #1

    select random entry from dictionary

    Hi

    How can I select a random entry from a dictionary, regardless of its
    key-values?

    regards tores


  • gaudetteje@gmail.com

    #2
    Re: select random entry from dictionary

    >From the Python 2.4 quickreference:

    d.popitem() Removes and returns an arbitrary (key, value) pair from
    d

    If this isn't random enough, then you can generate a random number in
    range(len(d))

    Comment

    • chengdu@gmail.com

      #3
      Re: select random entry from dictionary

      import random

      print d[d.keys()[int(random.rand om()*len(d.keys ()))]]

      HTH

      -c

      Comment

      • Peter Hansen

        #4
        Re: select random entry from dictionary

        gaudetteje@gmai l.com wrote:[color=blue][color=green]
        >>From the Python 2.4 quickreference:[/color]
        >
        > d.popitem() Removes and returns an arbitrary (key, value) pair from
        > d
        >
        > If this isn't random enough, then you can generate a random number in
        > range(len(d))[/color]

        Although this idea may suit the OP, "arbitrary" is
        most definitely not "random". Given the same
        dictionary on two separate occasions, this approach
        results in precisely the same sequence of items
        being returned.

        -Peter

        Comment

        • gaudetteje@gmail.com

          #5
          Re: select random entry from dictionary

          Peter,

          Agreed... which is why I said the 'random' module should be imported if
          more randomness is required.

          I only mentioned d.popitem() first in case Tores' application didn't
          need a psuedo-random item and instead he was looking to pull any value
          without randomness.

          Comment

          • Skip Montanaro

            #6
            Re: select random entry from dictionary

            >> d.popitem() Removes and returns an arbitrary (key, value) pair from d
            [color=blue][color=green]
            >> If this isn't random enough, then you can generate a random number in
            >> range(len(d))[/color][/color]

            Peter> Although this idea may suit the OP, "arbitrary" is most
            Peter> definitely not "random".

            Correct. The library reference is pretty clear about this too:

            Keys and values are listed in an arbitrary order which is non-random,
            varies across Python implementations , and depends on the dictionary's
            history of insertions and deletions.

            (This is from note 3 of http://docs.python.org/lib/typesmapping.html, which
            is referenced six times from the operations table on that page.)

            It's tempting to think it's random because you see the order change in
            seemingly unpredictable ways, but it's definitely not.

            Skip

            Comment

            Working...