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

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -