I have two lists, let's say lst1 = [4, 6, 11, 0, 1, 2, 5] and lst2 = [10, 3, 8]. I would like to list all permutations of inserting lst2 into lst1 such that the order of lst1 is maintained (order of lst2 need not be maintained). All elements in both lists are unique and order matters. Some potential valid results are
[10, 4, 6, 11, 0, 1, 3, 2, 8, 5]
[4, 6, 8, 10, 11, 0, 3, 1, 2, 5]
[4, 8, 6, 10, 11, 0, 1, 3, 2, 5] etc.
(lst1 elements are in order and lst2 elements may not be). Further, lst1 is circular, as is the resultant list containing all elements.
The total number of permutations of 7 (x) and 4(n) elements is given as the rising factorial --> (x+n-1)!/ (x-1)!. In this example, this would equal 7 * 8 * 9 * 10 = 5040 possibilities. error n=3 not 4, so the answer is 504. Thanks to Slothrop for catching the error!!
I tried the following, but it prints results that are not consistent with what I want. It prints lists without some elements of lst2 included in it. (I would like to do other operations for each of these permutations, so the result should not print results without including all elements of lst2.)
for locations in itertools.permutations(range(len(lst1) + len(lst2)-1), len(lst2)):
result = lst1[:]
for location, element in zip(locations, lst2):
result.insert(location, element)
print(result)
First generate all possible combinations of insertion locations. Need combination with replacement since we can repeatedly insert at the same index. Then zip it with all permutations of
lst2. Since the combinations are always sorted, we can adjust for the changing result length by keeping track of elements inserted.