title: generating permutation of a list by adding element of another list
body: <p>I try to write a python code that combines two different lists, C and C', to generate the possible permutation. We have some notions that I enumerate them below:
  1) F is a list which contains a pattern (I will show the pattern in the example).
  2) N is the number of elements in each generated sequence
  3) H is the number of elements to be added to C from C' to generate the sequences. </p>
  
  <p>For example,</p>
  
  <p>C=[6,6,5,4,3,2,1]</p>
  
  <p>C'=[1',2',4']</p>
  
  <p>H=3</p>
  
  <p>F=[6,4,5,2,1,3]</p>
  
  <p>N=10</p>
  
  <p>In this example, we want sequences with a length of ten by adding three elements of C' to C. Note, these three elements might be repeated elements as well, such as [1,2,1]. On the other hand, the generated sequence should satisfy F. There is a pattern in F. By pattern, I mean F[0] which is the first element of F, 6, should appear before F[1] which is the second element of F, 4, in the generated sequence. With that, we can use some rules to reduce the number of sequences that we need to generate. For example, we can only accept 6 in the first position, otherwise, the generated sequences do not satisfy F. We can accept numbers 6 and 4 in the second position, and so on. Lastly, the number of time each element appears in C need to be preserved. As an example, the generated sequences should hold, at least, two number 6, one number 5, 4,3,2,1.</p>
  
  <p>To make it clear I explain all in the example below:</p>
  
  <p>A possible generated sequences by combining C and C' is [6,4',6,5,2',4,1',3,2,1] where three elements of C', [4',2',1'], added to C.
  How the generated sequence satisfies F? If you start from left and iterate over elements and ignore the repeated elements, then we will have [6,4,5,2,1,3], which is equal to F (it satisfies F). Moreover, we have at least two number 6, one number 
  5, 4,3,2,1 in this example. As I mentioned, the number of elements in C should be preserved. For example, I can not generate a sequence like [6,4',5,2',4,1',3,2,1,1']. Indeed, the sequence satisfies F. </p>
  
  <p>The presented code does not take to account C' and H to generating the permutation. My code randomly generates the permutation by repeating every possible element of C (without combining C and C') by using itertools.product(C, repeat=N). The generated sequences have a length of 10, and they satisfy F. What is missing are 
  1) Considering C' and H to generate the sequences
  2) Preserving the rule of C ( the number of time each element appears in C need to be preserved.)</p>
  
  <pre class="lang-py prettyprint-override"><code>import itertools
  from collections import deque
  import collections
  from itertools import product
  
  def Generate_F_From_Lists(constrain):
    F_list=[]
    for u in constrain:
      if u in F_list:
        pass
      else:
        F_list.append(u)
    return F_list
  
  C= [6,6,5,4,3,2,1]
  N=10
  Satisfied_sequences=[]
  F = [6,4,5,2,1,3]
  # generate the permutation sequences of size N
  for p in itertools.product(C, repeat=N):
           # rules to reduce the number of generated permutation( for example in the first position only 6 is acceptable).
      for constrain in product([6] , [6],[5, 6] ,[4, 5, 6], [3, 4, 5, 6] , [2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]):
          Generated_F=Generate_F_From_Lists(constrain)
          if Generated_F==F:
              Satisfied_sequences.append(constrain)
  </code></pre>
  
  <p>Thank you in advance</p>