Home > Back-end >  Python - Replace string characters and get all combinations
Python - Replace string characters and get all combinations

Time:08-17

I want to map the dictionary d = {'R': ['a', 'g'], 'Y': ['c', 't']} to the string s = '----YY----RR----' to get the following output:

----cc----aa----
----cc----ag----
----cc----ga----
----cc----gg----
----ct----aa----
----ct----ag----
----ct----ga----
----ct----gg----
----tc----aa----
----tc----ag----
----tc----ga----
----tc----gg----
----tt----aa----
----tt----ag----
----tt----ga----
----tt----gg----

My (very) inefficient code is as below:

    seqs = set()
    for k,v in d.items():
        for i in v:
            i_seq = seq.replace(k,i,1)
            for n in v:
                n_seq = i_seq.replace(k,n,1)
                for k2,v2 in d.items():
                    for i2 in v2:
                        i2_seq = n_seq.replace(k2,i2,1)
                        for n2 in v2:
                            n2_seq = i2_seq.replace(k2,n2,1)
                            if not 'Y' in n2_seq and not 'R' in n2_seq:
                                seqs.add(n2_seq)

What is a smarter way to do that?

CodePudding user response:

A general approach without itertools:

def replaceCombinations(s, d):
  string_set = set([s])
  for i in range(len(s)):
    if s[i] in d.keys():
      new_set = set()
      for c in string_set:
        new_set.update(set(c.replace(s[i], new_char, 1) for new_char in d[s[i]]))
      string_set = new_set
  return string_set

string = "----YY----RR----"
d = {'R': ['a', 'g'], 'Y': ['c', 't']}
    
for c in sorted(replaceCombinations(string, d)):
  print(c)

CodePudding user response:

Use itertools.product:

from itertools import product
for p in product(product(d['R'],repeat=2),product(d['Y'],repeat=2)):
    print(f'----{p[0][0]}{p[0][1]}-----{p[1][0]}{p[1][1]}-----')
            
----aa-----cc-----
----aa-----ct-----
----aa-----tc-----
----aa-----tt-----
----ag-----cc-----
----ag-----ct-----
----ag-----tc-----
----ag-----tt-----
----ga-----cc-----
----ga-----ct-----
----ga-----tc-----
----ga-----tt-----
----gg-----cc-----
----gg-----ct-----
----gg-----tc-----
----gg-----tt-----
  • Related