vendredi 4 mars 2022

Python - How to randomly choose items from hash?

I have file elements.py where I have defined some elements

def get_random_element():
first = {
 a : 1,
 b : 2
}

second = {
 a : 2,
 b : 3,
 c : 4,
 d : 6,
 ....
}
...
return [ first, second, ... ]

In main python files, I am choosing random element from elements.py

from resources.elements import get_random_elements
...
element = random.choice(get_random_elements())

My problem is, that if it randomly choose second element (only second), I don't want there all items (a,b,c,d...), I want to choose it also randomly. So for example, if random generator takes second element, I want to also randomly choose its items e.g.:

a : 2
c : 4
d : 6
g : 9
...

Could someone help me, how to do it?




Aucun commentaire:

Enregistrer un commentaire