Generate all possible combinations of 2 characters of a potential 8-character string?

I need to generate all possible combinations of tuples of tuples

( (base1 , position1) , (base2 , position2) )

bases = ["U", "C", "A", "G"]and positions = [0,1,2,3,4,5,6,7,8].

Requirements

  • no repeatts
  • bases may be the same, but positions must be different
  • order must be saved.

For instance:

( (A,1), (B,2) ) == ( (B,2) , (A,1) )and ( (A,1), (B,1) )should be discarded.

Output Example:

[ ( (U,0) , (U,1) ), ( (U,0) , (U,2) ), ( (U,0) , (U,3) ) ...]

Must have a length of 448


Example:

For line length 2:

((U,0),(U,1))
((U,0),(C,1))
((U,0),(A,1))
((U,0),(G,1))

((C,0),(U,1))
((C,0),(C,1))
((C,0),(A,1))
((C,0),(G,1))

((A,0),(U,1))
((A,0),(C,1))
((A,0),(A,1))
((A,0),(G,1))

((G,0),(U,1))
((G,0),(C,1))
((G,0),(A,1))
((G,0),(G,1))

there would be all combinations ... I think


I still have it

all_possible = []
nucleotides = ["U","C","A","G"]


for i in range(len(nucleotides)):
    for j in range(8):
        all_possible.append(((nucleotides[i],j),(nucleotides[i],j)))
+4
source share
1 answer

It looks like you want a Cartesian product (all kinds of 2-base word) X (each 2-combination from range (8)).

You can get it at all

from itertools import product, combinations

def build(num_chars, length):
    bases = ["U", "C", "A", "G"]
    for letters in product(bases, repeat=num_chars):
        for positions in combinations(range(length), num_chars):
            yield list(zip(letters, positions))

what gives us

In [4]: output = list(build(2, 8))

In [5]: len(output)
Out[5]: 448

In [6]: output[:4]
Out[6]: 
[[('U', 0), ('U', 1)],
 [('U', 0), ('U', 2)],
 [('U', 0), ('U', 3)],
 [('U', 0), ('U', 4)]]

In [7]: output[-4:]
Out[7]: 
[[('G', 4), ('G', 7)],
 [('G', 5), ('G', 6)],
 [('G', 5), ('G', 7)],
 [('G', 6), ('G', 7)]]
+5
source

Source: https://habr.com/ru/post/1673554/


All Articles