I have 5 groups - A,B,C,D,E. I want to pick a random letter within the group letters, but I need exactly 4 instances of each letter
So in the end I want four As, four Bs, four Cs four Ds, and four Es, but I want to pick them randomly.
Using a dictionary was the best way I thought I could do this. I can keep track of how many letters I have this way, but I'm not certain how to write out code such that each letter only appears four times.
import random
random.seed(1)
groups = {
'A' : 0,
'B' : 0,
'C' : 0,
'D' : 0,
'E' : 0,
}
#return a random letter between A and E, the keys of the group dictionary
def random_letter():
return random.choice(list(groups.keys()))
while (groups['A'] != 4) and (groups['B'] != 4): #What can I put here so that it makes all of the groups A to E have only 4. using 'and' here isnt working im assuming because im using it incorrectly
groups[random_letter()] = 1
print(list(groups.keys())) #should return ['A', 'B', 'C', 'D', 'E']
print(list(groups.values())) #should return [4,4,4,4,4]
CodePudding user response:
I think this is the simplest way I could do it.
str_options = "abcde" * 4
options_as_list = list(str_options)
random.shuffle(options_as_list)
print(options_as_list)
CodePudding user response:
Why don't you use random.sample:
import random
res = random.sample('ABCDE', counts=[4]*5, k=20)
If all the letters always have the same number of occurrences:
res = random.sample('ABCDE'*4, 20)
CodePudding user response:
Copy the desired choices into a list and shuffle it only once:
import random
random.seed(1)
groups = {
'A': 0,
'B': 0,
'C': 0,
'D': 0,
'E': 0
}
full_list = [*groups]*4
random.shuffle(full_list)
print(full_list)
CodePudding user response:
from random import choice
groups = dict.fromkeys("ABCDE", 0)
while (key_pool := [key for key in groups if groups[key] != 4]):
key = choice(key_pool)
print(f"Incrementing {key}")
groups[key] = 1
Explanation: dict.fromkeys("ABCDE", 0) is a convenient short-hand for creating a dictionary, in this case, with the keys A through E, all mapping to the value 0.
while (key_pool := [key for key in groups if groups[key] != 4]): is pretty dense. We keep iterating as long as there exist keys which do not map to a count of 4. We generate that list of keys with the list comprehension, which we assign to key_pool via the so-called "walrus operator".
key = choice(key_pool) picks a random key from the collection of possible keys (keys that don't map to 4.)
groups[key] = 1 increments the value of the chosen key.
That being said, it's not immediately obvious from your question what you actually intend to do with this dictionary. Re-reading your question makes me wonder if what you really want is just a string with the letters A through E, each appearing four times in some random order?
If that's the case, maybe something like this?:
from random import sample
chars = "ABCDE"
print("".join(sample(chars*4, k=len(chars)*4)))
chars*4 will yield the string 'ABCDEABCDEABCDEABCDE'
random.sample will pick k characters from that string (when an element is picked, that element will not be considered in successive samples).
"".join(...) joins a collections of strings into a single string.
