从一个字典中获取值并将其产品放入其他字典中

时间:2016-07-26 08:27:55

标签: python-2.7

我有这两个这样的词典:

  

dict1 = {'C':'0.01464117821','E':'0.0621048942','D':'0.05465751502','G':'0.06693173594','F':'0.03875843054','我': '0.05478210779',''K':'0.05233761138','M':'0.02187736464','L':'0.09578482304','N':'0.04211449136',,'P':'0.05051274639','T': '0.0573250668',,'V':'0.06446141032','Y':'0.02954265733'}

     

dict2 = {'CVCVCVCV':['C','V','C','V','C','V','C','V'],'FEPFFTTK':[' F','E','P','F','F','T','T','K'],]}

我想要一个结果:

  

dict3 = {'CVCVCVCV':[来自dict1的C的值*来自dict1的V的值......依此类推],'FEPFFTTK':[来自dict1的F的值*来自dict1的E的值*来自P的值dict1 ......等等],]}

2 个答案:

答案 0 :(得分:0)

def product(x):
    a = 1
    for i in x:
        a *= i

    return a

dict1={'A': '0.08449754996', 'C': '0.01464117821', 'E': '0.0621048942', 'D': '0.05465751502', 'G': '0.06693173594', 'F': '0.03875843054', 'I': '0.05478210779', 'H': '0.02252015864', 'K': '0.05233761138', 'M': '0.02187736464', 'L': '0.09578482304', 'N': '0.04211449136', 'Q': '0.040185413', 'P': '0.05051274639', 'S': '0.07603976756', 'R': '0.05819425977', 'T': '0.0573250668', 'W': '0.01273081812', 'V': '0.06446141032', 'Y': '0.02954265733'}

dict2={'CVCVCVCV': ['C', 'V', 'C', 'V', 'C', 'V', 'C', 'V'], 'FEPFFTTK': ['F', 'E', 'P', 'F', 'F', 'T', 'T', 'K'], 'SHELRTPL': ['S', 'H', 'E', 'L', 'R', 'T', 'P', 'L'], 'DFGTGYS': ['D', 'F', 'G', 'T', 'G', 'Y', 'S'], 'QTNLLALN': ['Q', 'T', 'N', 'L', 'L', 'A', 'L', 'N'], 'PNGAGKTT': ['P', 'N', 'G', 'A', 'G', 'K', 'T', 'T']}

for key, values in dict2.items():
    for i in range(len(values)):
        values[i] = float(dict1[values[i]])

dict3 = {}

for key, values in dict2.items():
    dict3[key] = product(values)

答案 1 :(得分:0)

from __future__ import division
import numpy as np

dict1={'A': '0.08449754996', 'C': '0.01464117821', 'E': '0.0621048942', 'D': '0.05465751502', 'G': '0.06693173594', 'F': '0.03875843054', 'I': '0.05478210779', 'H': '0.02252015864', 'K': '0.05233761138', 'M': '0.02187736464', 'L': '0.09578482304', 'N': '0.04211449136', 'Q': '0.040185413', 'P': '0.05051274639', 'S': '0.07603976756', 'R': '0.05819425977', 'T': '0.0573250668', 'W': '0.01273081812', 'V': '0.06446141032', 'Y': '0.02954265733'}

dict2={'CVCVCVCV': ['C', 'V', 'C', 'V', 'C', 'V', 'C', 'V'], 'FEPFFTTK': ['F', 'E', 'P', 'F', 'F', 'T', 'T', 'K'], 'SHELRTPL': ['S', 'H', 'E', 'L', 'R', 'T', 'P', 'L'], 'DFGTGYS': ['D', 'F', 'G', 'T', 'G', 'Y', 'S'], 'QTNLLALN': ['Q', 'T', 'N', 'L', 'L', 'A', 'L', 'N'], 'PNGAGKTT': ['P', 'N', 'G', 'A', 'G', 'K', 'T', 'T']}

for key, values in dict2.items():
    for i in range(len(values)):
        values[i] = float(dict1[values[i]])

dict3 = {}

for key, values in dict2.items():
    dict3[key] = np.product(values)