如何在Python中从另一个字典中提取和分割值?

如何在Python中从另一个字典中提取和分割值?,第1张

概述sample = [['CGG','ATT'],['GCGC','TAAA']]#Frequencies of each base in the paird1 = [[{'G': 0.66, 'C': 0.33}, {'A': 0.33, 'T': 0.66}], [{'G': 0.5, 'C': 0.5}, {'A': 0.75, 'T': 0.25}]]#Frequencies of
sample = [['CGG','ATT'],['GCGC','TAAA']]#FrequencIEs of each base in the paird1 = [[{'G': 0.66,'C': 0.33},{'A': 0.33,'T': 0.66}],[{'G': 0.5,'C': 0.5},{'A': 0.75,'T': 0.25}]]#FrequencIEs of each pair occurring togetherd2 = [{('C','A'): 0.33,('G','T'): 0.66},{('G','T'): 0.25,('C','A'): 0.5,'A'): 0.25}]

问题:

考虑第一对:[‘CGG’,’ATT’]

如何计算a,其中a是:

float(a) = (freq of pairs) - ((freq of C in CGG) * (freq of A in ATT))eg. in CA pairs,float (a) = (freq of CA pairs) - ((freq of C in CGG) * (freq of A in ATT))Output a = (0.33) - ((0.33) * (0.33)) = 0.222222

计算任何一个组合的“a”(CA对或GT对)

Final Output for sample : a = [0.2222,- 0.125]

如何计算b,其中b是:

float (b) = (float(a)^2)/ (freq of C in CGG) * (freq G in CGG) * (freq A in ATT) * (freq of T in ATT)Output b = 1

对整个列表执行此 *** 作

Final Output for sample : b = [1,0.3333]

我不知道如何从d1和d2中提取所需的值并执行数学运算.

我试着为a的值编写以下代码

float a = {k: float(d1[k][0]) - d2[k][0] * d2[k][1]for k in d1.vIEwkeys() & d2.vIEwkeys()}

但是,它不起作用.另外,我更喜欢for循环而不是理解

我尝试为上面写的(一个非常有缺陷的)for循环:

float_a = []for pair,i in enumerate(d2):    for base,j in enumerate(d1):        float (a) = pair[i][0] - base[j][] * base[j+1][]        float_a.append(a)float_b = []  for floata in enumerate(float_a):    for base,j in enumerate(d1):        float (b) = (float(a) * float(a)) - (base[j] *    base[j+1]*base[j+2]*base[j+3])        float_b.append(b)
解决方法 通常当有多个公式和中间步骤这样的棘手问题时,我喜欢通过将工作分成几个函数来模块化它.以下是生成的注释代码,用于处理原始问题和注释中的案例:

from collections import Counterdef get_base_freq(seq):    """    Returns the normalized frequency of each base in a given sequence as a dictionary.    A dictionary comprehension converts the Counter object into a "normalized" dictionary.    """    seq_len = len(seq)    base_counts = Counter(seq)    base_freqs = {base: float(count)/seq_len for base,count in base_counts.items()}    return base_freqsdef get_pair_freq(seq1,seq2):    """    Uses zip to merge two sequence strings together.    Then performs same counting and normalization as in get_base_freq.    """    seq_len = len(seq1)    pair_counts = Counter(zip(seq1,seq2))    pair_freqs = {pair: float(count)/seq_len for pair,count in pair_counts.items()}    return pair_freqsdef calc_a(d1,d2):    """    Arbitrarily takes the first pair in d2 and calculates the a-value from it.    """    first_pair,pair_freq = d2.items()[0]    base1,base2 = first_pair    a = pair_freq - (d1[0][base1]*d1[1][base2])    return adef calc_b(a,d1):    """    For this calculation,we need to use all of the values from d1 and multiply them together.    This is done by merging the two sequence half-results together and multiplying in a for loop.    """    denom_ACGT = d1[0].values() + d1[1].values()    denom = 1    for val in denom_ACGT:        denom *= val    b = a*a/float(denom)    return bif __name__ == "__main__":    sample = [['CGG','TAAA'],['ACAA','CAAC']]    b_result = []    for seq_pair in sample:        d1 = [get_base_freq(seq) for seq in seq_pair]        d2 = get_pair_freq(*seq_pair)        a = calc_a(d1,d2)        b = calc_b(a,d1)        b_result.append(b)    print b_result

如果有任何事情需要澄清,或者如果我没有考虑的情况失败,请告诉我!

总结

以上是内存溢出为你收集整理的如何在Python中从另一个字典中提取和分割值?全部内容,希望文章能够帮你解决如何在Python中从另一个字典中提取和分割值?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1197733.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存