jeudi 26 mars 2020

Efficient way to generate a random alphabet in Python skewed towards popular letters? [duplicate]

I want to generate a random letter but the randomness is skewed towards how popular the letter is. For example 'E', is the most common letter in the English alphabet, so it should have the highest probability.

I found this frequency table to define the popularity of a letter.

My naive approach is as follows:

import random

popularity_dict =  {'E': 21912, 'T': 16587, 'A': 14810, 'O': 14003, 'I': 13318, 'N': 12666, 'S': 11450, 'R': 10977, 'H': 10795, 'D': 7874, 'L': 7253, 'U': 5246, 'C': 4943, 'M': 4761, 'F': 4200, 'Y': 3853, 'W': 3819, 'G': 3693, 'P': 3316, 'B': 2715, 'V': 2019, 'K': 1257, 'X': 315, 'Q': 205, 'J': 188, 'Z': 128}
#scraped from the given link

letter_list = []
for letter, popularity in popularity_dict.items():
    letter_list.extend([ letter] * popularity)


print(random.choice(letter_list))

I was wondering if there was a more effiecient approach to the problem




Aucun commentaire:

Enregistrer un commentaire