Reputation: 13
Lets say I have the following list:
lst = [0,1,3,a,b,c]
I would like the final outcome to be all possible permutations of lst
, but be 20 characters long.
I have looked and can only find examples that would create a final outcome that would be 6 or less in length.
Any ideas?
Upvotes: 0
Views: 1824
Reputation: 32300
I think itertools.product
is what you're looking for.
# A simple example
import itertools
lst = [0, 1]
print(list(itertools.product(lst, repeat=2)))
# [(0, 0), (0, 1), (1, 0), (1, 1)]
Note that itertools.product
itself returns an itertools.product
object, not a list.
# In your case
import itertools
lst = [0, 1, 3, a, b, c]
output = list(itertools.product(lst, repeat=20))
Upvotes: 3
Reputation: 6957
list(permutations(lst,x))
; where lst
is iterable (your input list) and x
is the no of elements
for example:
In [8]: lst = [0, 1, 3, 'a', 'b', 'c']
In [9]: from itertools import permutations
In [10]: result=[list(permutations(lst,x)) for x in range(1,len(lst))]
Upvotes: 0
Reputation: 765
The problem is that permutations and combinations (if you use itertools) follow the definition that each result can only physically exist if it is of equal or lesser length than the originating list.
If you are suggesting that you want one of the valid results to be "013abc013abc013abc01", then you'll have to modify your list to just be 20 items long and be made up of those 6 values.
from itertools import permutations
i = [0,1,3,'a','b','c',0,1,3,'a','b','c',0,1,3,'a','b','c',0,1]
results = []
for a in permutations(i, 20):
results.append(a)
Upvotes: 0
Reputation: 4107
Are you sure you want all permutations
? Assuming you want to reuse characters (not a permutation) that would be a list with a length of 6^20.
If you want one string of length 20 built from characters in that list, this should do the job:
from random import choice
''.join(choice(chars) for _ in range(length))
Upvotes: 0