关于.split()的快速思考

2024-09-30 01:29:09 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我需要用python制作一个punnett正方形。punnett平方基本上是一种简单的方法来确定可见和有时不可见的特征。到目前为止,我的代码采用了双亲的基因组成,找到了A和G的所有不同组合。目前我遇到的唯一问题是,当我打印时,字母的顺序不正确。例如:对于每个“孩子”可能的基因组成,有两个A(大写或小写)和两个G(大写或小写)。我做了大量的研究,关于软件的其他问题/答案中唯一一个与我有关的问题/答案并不清楚,也不起作用。我的代码如下。你知道吗

import itertools
import string

#genepool_1 refers to parent 1's alleles
#genepool_2 refers to parent 2's alleles
#outerlayerf1 refers to both parent genetic contributions to the first generation of offspring
#f1 refers to the punnett square of the first generation of offspring

#parent 1
genepool_1 = ['Aa','Gg']
parent_1 = sorted(list(itertools.product(*genepool_1)))

#parent 2
genepool_2 = ['aa','GG']
parent_2 = sorted(list(itertools.product(*genepool_2)))


#F1 or Parent 1/2 Offspring
outerlayerf1 = [parent_1,parent_2]
f1___________ = list(itertools.product(*outerlayerf1))
f1__________ = str(f1___________)
f1_________ = f1__________.replace('[','')
f1________ = f1_________.replace(']','')
f1_______ = f1________.replace("'",'')
f1______ = f1_______.replace(' ','')
f1_____ = f1______.replace(')),((', ') (')
f1____ = f1_____.replace('((', '(')
f1___ = f1____.replace('))',')')
f1__ = f1___.replace('),(','')
f1_ = f1__.replace(',','')
print f1_

然后打印出来

(AGaG) (AGaG) (AGaG) (AGaG) (AgaG) (AgaG) (AgaG) (AgaG) (aGaG) (aGaG) (aGaG) (aGaG) (agaG) (agaG) (agaG) (agaG)

当它应该打印时

(AaGG) (AaGG) (AaGG) (AaGG) (AagG) (AagG) (AagG) (AagG) (aaGG) (aaGG) (aaGG) (aaGG) (aagG) (aagG) (aagG) (aagG)

我知道每个选项打印4次。必须这样才能获得最准确的概率

非常感谢

伊莱


Tags: toreplaceparentf1itertoolsrefersaaggagag
2条回答

我对这个问题的所有相关内容都感到非常困惑,但我想我是为了解决这个问题,所以至少我希望这能有所帮助:

f1 = [
     thingies[0][0] + thingies[1][0] + thingies[0][1] + thingies[1][1]
     for thingies in zip(parent_1, parent_2)
] * 4
print(f1)

另一种方法是使用zip()函数和str.jon()-

>>> genepool_1 = ['Aa','Gg']
>>> parent_1 = sorted(list(itertools.product(*genepool_1)))
>>>
>>> #parent 2
... genepool_2 = ['aa','GG']
>>> parent_2 = sorted(list(itertools.product(*genepool_2)))
>>>
>>>
>>> #F1 or Parent 1/2 Offspring
... outerlayerf1 = [parent_1,parent_2]
>>> f1 = list(itertools.product(*outerlayerf1))
>>> f2 = [''.join(''.join(i) for i in list(zip(*x))) for x in f1]
['AaGG', 'AaGG', 'AaGG', 'AaGG', 'AagG', 'AagG', 'AagG', 'AagG', 'aaGG', 'aaGG', 'aaGG', 'aaGG', 'aagG', 'aagG', 'aagG', 'aagG']

相关问题 更多 >

    热门问题