【问题标题】:Quick thought regarding .split()关于 .split() 的快速思考
【发布时间】:2015-07-20 14:51:38
【问题描述】:

所以我需要使用python制作一个punnett square。 punnett square 基本上是一种确定可见和有时不可见特征的简单方法。到目前为止,我的代码采用了父母双方的基因构成,并找到了 A 和 G 的所有不同组合。此时我遇到的唯一问题是,当我打印时,字母的顺序不正确。例如:对于每个“孩子”可能的基因构成,有两个 A(大写或小写)和两个 G(大写或小写)。我已经做了相当多的研究,唯一与我的有点相关的关于 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 次。需要这样才能获得最准确的概率

非常感谢

伊莱

【问题讨论】:

  • 这是一些奇怪的变量命名约定
  • 哈哈,是的,我知道我想不出更好的了,下划线的数字越来越多@PadraicCunningham
  • 也许 f_1, f_2f1_1, f1_2 .. 可能更容易理解 ;)
  • sorted(list(iterator)) 的所有调用是怎么回事?排序返回一个列表.....
  • 或者只是将其保留为列表....

标签: python genetics


【解决方案1】:

我对与这个问题相关的一切感到非常困惑,但我认为我可以解决这个问题,所以至少我希望这会有所帮助:

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

【讨论】:

  • 非常感谢您的工作。很抱歉造成混乱!
【解决方案2】:

使用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']

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 1970-01-01
    • 2010-10-19
    • 2011-06-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多