random choice from dictionary

Avi Gross avigross at verizon.net
Mon Dec 24 00:53:17 EST 2018


I did say some would use enumerate but I was rushing to go to my nephews wedding and my enum became emum which is meaningless 😊

If the goal is to make a relatively efficient algorithm and the index made by enumerate is not needed, then I judged that the overhead of enumerate(data) returning two values to be unpacked each time through the loop might be more than having a single counter. Of course enumerate likely is considered more pythonic 😊

Similarly, the suggestion to use islice (although the name is also unfortunately reminiscent of is lice) may be a good one. But it basically does what my function did, perhaps as fast and perhaps not. My impression is it iterates using next() a number of times then stops. It is so simple, there might be no need for my function. The request was to find an nth item and you can feed (the random) n directly into islice.

Just for amusement, I already get a random word a day in my email from two dictionary sources in English as well as quite a few in other languages. I not only get the word as a key, but also a translation and even an example sentence with translation.

But I doubt the words are chosen randomly and they are not likely to be from a python dictionary.😊

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On Behalf Of MRAB
Sent: Sunday, December 23, 2018 4:21 PM
To: python-list at python.org
Subject: Re: random choice from dictionary

On 2018-12-23 19:52, Avi Gross wrote:
> There are quite a few places the new pythonic way of doing things 
> requires extra steps to get an iterator to expand into a list so 
> Abdul-Rahmann probably is right that there is no easy way to get a 
> random key from a standard dictionary. Other than the expected answers 
> to make a customized dictionary or function that provides the 
> functionality at the expense for potentially generating a huge list of 
> keys in memory, I offer the following as an alternative for large 
> dictionaries and especially if they have large keys.
> 
> The following is a function that iterates over the dictionary one key 
> at a time and when it reaches a random one, it returns the key without 
> further evaluation. On the average, it takes N/2 iterations for N 
> keys. Asking to make a list(data) may be efficient in terms of time 
> and indexing is fast but space is used maximally unless it just points to existing storage.
> 
> Here is one way to do the function. Some might use an emum instead.
> 
> def rand_key(data):
>      """Get a random key from a dict without using all of memory."""
>      import random
>      randy = random.randrange(len(data))
>      this = 0
>      for key in data:
>          if (this == randy):
>              return(key)
>          this += 1
> 
[snip]
You might as well use 'enumerate' there:

def rand_key(data):
     """Get a random key from a dict without using all of memory."""
     import random
     randy = random.randrange(len(data))
     for this, key in enumerate(data):
         if this == randy:
             return key

Or you could use 'islice':

def rand_key(data):
     """Get a random key from a dict without using all of memory."""
     import itertools
     import random
     randy = random.randrange(len(data))
     return list(itertools.islice(data, randy, randy + 1))[0]
--
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list