Last active
July 25, 2023 13:23
-
-
Save yerttle/dbdb879ca3f55acee482165b02b1d553 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
hobbies = ['a', 'b', 'c'] | |
hobbies_wieghts = [70, 20, 10] | |
def choice_hobby(): | |
# list enumerate here wraps the arrary in a list so choices returns the indexes | |
c = random.choices(list(enumerate(hobbies)), weights=hobbies_wieghts, k=1) | |
# For the first item in the list get the first item in the inner list | |
arrary_index = c[0][0] | |
# For the first item in the list get the second item in the inner list | |
array_value = c[0][1] | |
# Set the wieght for this item to 0 so it is never choosen again | |
hobbies_wieghts[arrary_index] = 0 | |
# Return the value | |
return array_value | |
for i in range(len(hobbies)): | |
print(choice_hobby()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment