python - Get a list of N items with K selections for each element? -


for example if have selection set k

k = ['a','b','c'] 

and length n

n = 4 

i want return possible:

['a','a','a','a'] ['a','a','a','b'] ['a','a','a','c'] ['a','a','b','a'] ... ['c','c','c','c'] 

i can recursion not interesting. there more pythonic way?

that can done itertools.

>>> k = ['a','b','c'] >>> import itertools >>> n = 4 >>> = itertools.product(k,repeat = n) >>> l = [a in i] >>> l[:3] [('a', 'a', 'a', 'a'), ('a', 'a', 'a', 'b'), ('a', 'a', 'a', 'c')] 

edit: realized want product, not combinations_with_replacement. updated code.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -