匹配密码子

时间:2015-09-21 06:04:27

标签: python pool

所以我现在正在学习嵌套池,但我并不是真的能够理解它。 我的任务是按以下格式返回所有匹配对:

[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]

所以这是我的代码:

def matching_codons(complements, poolA, poolB):
complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
poolB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
final = []
 for i in poolA:
  for z in poolB:
    if i and z in complements:
      final.append()
      return(final)

它没有工作,我不知道为什么,我不太明白。如何根据提供的字典创建一个语句,以便poolA和poolB匹配?

2 个答案:

答案 0 :(得分:0)

尝试在另一个池中搜索补充,而不是遍历poolA和poolB中的每个元素。

def _complements(complements, str):
    return ''.join([complements[i] for i in str])

def matching_codons(complements, poolA, poolB):
    final = []
    for i in poolA:
        if _complements(complements, i) in poolB:
            final.append((i, _complements(complements, i)))
    return final
complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
poolB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CCC', 'CCG', 'GTA']
print matching_codons(complements, poolA, poolB)

<强>输出

[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]

答案 1 :(得分:0)

>>> final = []
>>> for a in poolA:
...     for b in poolB:
...         count = 0
...         for x in range(len(a)):
...             if complements[a[x]] == b[x]:
...                 count += 1
...             else:
...                 break
...             if count == 3:
...                 final.append((a,b))
... 
>>> final
[('AAG', 'TTC'), ('GAT', 'CTA'), ('TTG', 'AAC'), ('CAT', 'GTA'), ('GGC', 'CCG'), ('ATT', 'TAA'), ('TCT', 'AGA')]