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

'hasOwnProperty' in javascript -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -

java - How to implement an entity bound odata action in olingo v4.3 -