本文介绍了Punnett Square函数:列表中的匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到特定十字架的所有可能配子.

I want to find all the possible gametes of a specific cross.

例如:'AABB' x 'aabb'将被分为['AA', 'BB']['aa', 'bb'].我已经完成了那部分.之后,它应该返回:[['Aa', 'Aa', 'Aa', 'Aa'], ['Bb', 'Bb', 'Bb', 'Bb']](对于父级"A"中的每个等位基因,它都与父级"B"中的等位基因匹配;这是简化的Punnett平方).

For example: 'AABB' x 'aabb' would be split into ['AA', 'BB'] and ['aa', 'bb']. I have that part done. After this, it should return:[['Aa', 'Aa', 'Aa', 'Aa'], ['Bb', 'Bb', 'Bb', 'Bb']] (for every allele in parent 'A', it matches with an allele in parent 'B'; this is a simplified Punnett Square).

这是我到目前为止所拥有的:

This is what I have so far:

def punnett(a, b):
    n = int(len(a)/2)
    x = int(float(len(a)) / n)
    partsA, partsB, gametes = [a[i * x : i * x + x] for i in range(n)], [b[i * x : i * x + x] for i in range(n)], []
    for y in range(1, n):
        g = []
        for index in range(0, n/2 + y):
            for i in partsA[index]:
                for j in partsB[index]:
                    g.append(i+j)
        gametes.append(g)
    return gametes

它并没有达到我的预期,

It doesn't result in what I expected it would, though:

>>> punnett('AaBb', 'AaBb')
[['AA', 'Aa', 'aA', 'aa', 'BB', 'Bb', 'bB', 'bb']]

此外,三杂种杂交也不能给出我期望的结果,或者:

Also, a trihybrid cross does not give the results I expected, either:

>>> punnett('AaBbCc', 'AaBbCc')
[['AA', 'Aa', 'aA', 'aa', 'BB', 'Bb', 'bB', 'bb'], ['AA', 'Aa', 'aA', 'aa', 'BB', 'Bb', 'bB', 'bb', 'CC', 'Cc', 'cC', 'cc']]

如果我可以就自己在做错的事情以及如何改进它提出意见,那就太好了.谢谢!

If I could get input on what I'm doing wrong and how I could improve it, that would be great. Thanks!

推荐答案

好的,我只是阅读了一些您可能想要实现的材料,这就是更改

OK, I just read some materials on what you might want to achieve, and here is the changes

>>> from itertools import product, izip

如果我理解正确,则等位基因是忽略大小写的连续字符.如果是"AaBb",则其['Aa'和'Bb']

If I understand correctly, an allele is consecutive characters ignoring case. In case of "AaBb" its ['Aa', and 'Bb']

>>> def allele(e):
    return [list(v) for _, v in groupby(e, key = str.lower)]

Cross被定义为等位基因在材料和亲本之间的所有可能的交换

A Cross is defined as all possible crossover of the allele between the material and the parent

随后从交叉选择新的基因组

Followed by selection of the new genome from the crossover

>>> def punnett(a, b):
    return [''.join(e)
        for e in product(*([''.join(e) for e in product(*e)]
                    for e in izip(allele(a), allele(b))))]

>>> punnett('AaBbCc', 'AaBbCc')
['AABBCC', 'AABBCc', 'AABBcC', 'AABBcc', 'AABbCC', 'AABbCc', 'AABbcC', 'AABbcc', 'AAbBCC', 'AAbBCc', 'AAbBcC', 'AAbBcc', 'AAbbCC', 'AAbbCc', 'AAbbcC', 'AAbbcc', 'AaBBCC', 'AaBBCc', 'AaBBcC', 'AaBBcc', 'AaBbCC', 'AaBbCc', 'AaBbcC', 'AaBbcc', 'AabBCC', 'AabBCc', 'AabBcC', 'AabBcc', 'AabbCC', 'AabbCc', 'AabbcC', 'Aabbcc', 'aABBCC', 'aABBCc', 'aABBcC', 'aABBcc', 'aABbCC', 'aABbCc', 'aABbcC', 'aABbcc', 'aAbBCC', 'aAbBCc', 'aAbBcC', 'aAbBcc', 'aAbbCC', 'aAbbCc', 'aAbbcC', 'aAbbcc', 'aaBBCC', 'aaBBCc', 'aaBBcC', 'aaBBcc', 'aaBbCC', 'aaBbCc', 'aaBbcC', 'aaBbcc', 'aabBCC', 'aabBCc', 'aabBcC', 'aabBcc', 'aabbCC', 'aabbCc', 'aabbcC', 'aabbcc']
>>> punnett('AaBb', 'AaBb')
['AABB', 'AABb', 'AAbB', 'AAbb', 'AaBB', 'AaBb', 'AabB', 'Aabb', 'aABB', 'aABb', 'aAbB', 'aAbb', 'aaBB', 'aaBb', 'aabB', 'aabb']
>>> 

这篇关于Punnett Square函数:列表中的匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 02:50