-3

I have my input like this:

T A G C  
2  

I want my output like this.

TT
TA
TG
TC
AT
AA
AG
AC
GT
GA
GG
GC
CT
CA
CG
CC   

If i give 3 in the input at place of 2 the the output should be like:

TTT
TTA
TTG
TTC
TAT
TAA
TAG
TAC
...
...
and so on....

I have written code but i dont know what to do after this:

data=open('D:\python\input.txt')
a=data.read().split('\n')
result=[]
symbol=a[0]
integer=a[1]

if integer==0:
   result.append(None)
for s in symbol: 

I dont know what to do after this ....

  • can you tell me where is the duplicate ques ?? – shahbaz khan Feb 22 '15 at 09:15
  • It gets added to a yellow box at the top of your question. But it's also right here: http://stackoverflow.com/questions/533905/get-the-cartesian-product-of-a-series-of-lists-in-python – Bill Lynch Feb 22 '15 at 09:15

1 Answers1

2

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))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321