views:

200

answers:

3

I'm trying to create a possible list of codons given a protein sequence.

Basically, the script i'm trying to create will process a given string input and outputs a possible combinations of another set of strings the input represents.

For example, the character 'F' represents either 'UUU' or 'UUC'; the character 'I' represents either 'AUU', 'AUC', or 'AUA'.

Given the input 'FI', the script I'm trying to create should output: 'UUUAUU', 'UUUAUC', 'UUUAUA', 'UUCAUU', 'UUCAUC', and 'UUCAUA'.

I'm currently stuck with this code:

import itertools

F = ['UUU', 'UUC']
I = ['AUU', 'AUC', 'AUA']

seq, pool = 'FI', []

for i in seq:
   pool.append(eval(i))

for n in itertools.product(pool):
   print(n)

It works when I replace pool in itertools.product with pool[0], pool[1]. But I can't figure out how to make it work so that the user can input a string with more than 2 character (i.e. not to make it hard-coded).

Thanks in advance for the help!

+2  A: 

You can use *pool to "unpack" the list when calling product():

for n in itertools.product(*pool):
   print(n)

This syntax expands the list pool into separate positional parameters.

sth
A: 

itertools.product(pool[0],pool[1],...pool[len(pool)-1]) is equivalent to itertools.product(*pool)

import itertools

F = ['UUU', 'UUC']
I = ['AUU', 'AUC', 'AUA']

pool=[F,I]

for n in itertools.product(*pool):
   print(''.join(n))
gnibbler
A: 

I am stuck with the same problem but I want product of both F and I too without using itertools is it possible?

@user343934 Ask a new question instead of bumping this one.
aaronasterling