python - Get a random sample with replacement -
i have list:
colors = ["r", "g", "b", "y"]
and want 4 random letters it, including repetition.
running give me 4 unique letters, never repeating letters:
print(random.sample(colors,4))
how list of 4 colors, repeating letters possible?
with random.choice
:
print([random.choice(colors) _ in colors])
if number of values need not correspond number of values in list, use range
:
print([random.choice(colors) _ in range(7)])
from python 3.6 onwards can use random.choices
(plural) , specify number of values need k argument.
Comments
Post a Comment