关于.split()的快速思考

时间:2015-07-20 14:51:38

标签: python genetics

所以我需要使用python制作一个punnett square。 punnett square基本上是一种确定可见和有时不可见特征的简单方法。到目前为止,我的代码采用了父母的两个遗传构成,并找到了A和G的所有不同组合。我现在唯一的问题是,当我打印时,这些字母的顺序并不正确。例如:对于每个"孩子"可能的遗传构成,有两个A(大写或小写)和两个G(也可以是大写或小写)。我已经做了相当多的研究,而且与SOF有关的另一个关于SOF的问题/答案并不清楚,也没有用。我的代码如下。

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次。需要这样才能获得最准确的概率

非常感谢

2 个答案:

答案 0 :(得分:1)

我对与这个问题相关的所有内容感到非常困惑,但我认为我已经得到了这个,所以至少我希望有所帮助:

<style type="text/css">
ul {font-weight:bold;}
ol {font-style:italic;}
</style>

答案 1 :(得分:1)

使用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']
相关问题