我想在计算之后但在将它们存储在“概率”字典之前将我的答案四舍五入到小数点后四位。我已经尝试在 print (probs) 语句和上面的行中使用“round”函数,但错误是这个 dict 不允许使用 round方法。HPRD_db = sequence.readFastaFile('HPRD.fa', sequence.Protein_Alphabet)# Your code to construct HPRD background belowcounts = {'A':0,'C':0,'D':0,'E':0,'F':0,'G':0,'H':0,'I':0,'K':0,'L':0,'M':0,'N':0,'P':0,'Q':0,'R':0,'S':0,'T':0,'V':0,'W':0,'Y':0}total = 0for seq in HPRD_db: for nt in seq.alphabet: counts[nt] += seq.count(nt)for nt in counts: total += counts[nt]probs = {}for nt in counts: probs[nt] = counts[nt] / float(total)print(probs)概率字典的输出是:{'A': 0.06984578386497987, 'C': 0.023122616793955175, 'D': 0.04814769879543413, 'E': 0.07064443899675797, 'F': 0.03724399454951442, 'G': 0.06608386984000873, 'H': 0.026116296117699497, 'I': 0.04427741198662404, 'K': 0.05762853493424018, 'L': 0.09913638061282196, 'M': 0.02165875240348138, 'N': 0.0366184744463036, 'P': 0.06165068623619301, 'Q': 0.04719159416959176, 'R': 0.05605356467437009, 'S': 0.08174647019029524, 'T': 0.052927746605418616, 'V': 0.060404874541272385, 'W': 0.012089388058578817, 'Y': 0.02741142218245913}
1 回答
慕姐8265434
TA贡献1813条经验 获得超2个赞
像这样使用 round() 函数:
for nt in counts: probs[nt] = round(counts[nt] / float(total), 4) print(probs)
添加回答
举报
0/150
提交
取消