0

我写了以下一段代码:

def all_di(fl):
    dmm = {}
    for k in range(2):
        for i in fl:
            for m in range (len(i)-1):
                temp = i[m:m+k+1]
                if temp in dmm:
                    dmm[temp] += 1.0
                else:
                    dmm[temp] = 1.0
##  return dmm
    p = raw_input("Enter a 2 AA long seq:")
    sum = 0
    for x,y in dmm.iteritems():
        if x == p:
            n1 = y
    for l,m in dmm.iteritems():
        if l[0] == p[0]:
            sum = sum + m
    print float(n1)/float(sum)

all_di(inh) 

如果 inh ={'VE':16,'GF':19,'VF':23,'GG' :2}

该代码的工作原理如下:

Enter a 2 AA long seq: VE

结果将是 = 16/(16+23)=0.41

工作原理:该函数在字典中搜索与dmm输入的键相似的键input(此处为“VE”示例)。它存储它的值,然后搜索所有第一个字母相同的键值对,并将其所有值相加并返回一个分数。

VE = 16
**V**E + **V**F = 39
= 16/39 = 0.41

我想要什么:保持函数完整,我想要一个辅助字典,它迭代字典中的每个键值对,并将它的小数值存储在不同的字典中,这样:

new_dict = {'VE' : 0.41, 'GF':0.90,'VF':0.51, 'GG': 0.09}

我不想删除该print语句,因为它是我的程序的输出。然而,我需要new_dict进一步的工作。

4

1 回答 1

1
def all_di(fl,p=0):
  dmm = {}
  interactive = p == 0
  if interactive:
    p = raw_input("Enter a 2 AA long seq:")
  if p in fl: 
    numer = fl[p]
    denom = 0.0 
    for t in fl: 
      if t[0] == p[0]:
        denom = denom + fl[t]
  if interactive:
    print numer / denom
  return numer / denom

inh = {'VE':16,'GF':19,'VF':23,'GG' :2} 
all_di(inh)
new_dict =  {x:all_di(inh, x) for x in inh}
print new_dict
于 2013-03-27T01:40:57.990 回答