You can use itertools.product, with a repeat
of n
:
from itertools import product
n = 2
l = ["T","A","G","C"]
for prod in (product(l,repeat=n)):
print("".join(prod))
TT
TA
TG
TC
AT
AA
AG
AC
GT
GA
GG
GC
CT
CA
CG
CC
You should also use with
to open your files which closes them automatically and either use raw string r
for your file paths or /'s
. You need to split each line into individual elements, you can then use itertools.chain.from_iterable
to join all the elements and create the product:
from itertools import product, chain
n = 3
with open(r'D:\python\input.txt') as data: # <- raw string "r"
symbols = chain.from_iterable(x.split() for x in data)
for prod in product(symbols,repeat=n):
print("".join(prod))
TTT
TTA
TTG
TTC
TAT
TAA
.......................
Without chain and a generator expressions we can use list.extend
t create a single list of all items:
with open(r'D:\python\input.txt') as data:
symbols = []
for line in data:
symbols.extend(line.split())
for prod in product(symbols, repeat=n):
print("".join(prod))