开发者

Working with itertools.product and lists in python 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!


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.


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))


Sometimes (in my opinion more often) the list does not need a printout:

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

pool=[F,I]
lista = []

for n in itertools.product(*pool):
      lista.append(''.join(n))

lista

Working with itertools.product and lists in python 3

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜