I am trying to make all possible substitutions between a reference and a test sequence. The sequences will always be the same length and the goal is to substitute Test characters with those of Ref.
Ref= "AAAAAAAAA"
Test="AAATAATTA"
Desired output:
AAATAATTA, AAATAAAAA, AAATAATAA, AAATAATTA, AAAAAATTA, AAAAAATAA, AAAAAAATA
You can use
itertools.productfor this if youzipthe two strings together (turning them into a set of 2-tuples forproductto find combinations of). You then probably want to uniquify them in a set. All together it looks like this:To break that down a little further, since it looks a bit like line noise if you aren't familiar with the functions in question...
Here's the
zipthat turns our two strings into an iteration of pairs (wrapping it in a list comprehension for easy printing, but we'll remove that in the next stage):The
productfunction takes an arbitrary number of iterables as arguments; we want to feed it all of our 2-tuples as separate arguments using*:Use
jointo turn those tuples back into strings:And by making this a set comprehension (
{}) instead of a list comprehension ([]), we get just the unique elements.