匹配两个嵌套字典之间的值

时间:2017-06-18 12:05:26

标签: python python-2.7

我是Python的新手,我正在尝试找到问题的解决方案。 我有两个嵌套的词典:`

dna_type = {"hair":{"black":"CCAGCAATCGC","brown":"GCCAGTGCCG","blonde":"TTAGCTATCGC"},
            "face":{"square":"GCCACGG","round":"ACCACAA","oval":"AGGCCTCA"},
            "eye":{"blue":"TTGTGGTGGC","green":"GGGAGGTGGC","brown":"AAGTAGTGAC"},
            "gender":{"female":"TGAAGGACCTTC","male":"TGCAGGAACTTC"},
            "race":{"white":"AAAACCTCA","black":"CGACTACAG","asian":"CGCGGGCCG"}}

suspects = {"Eva":{"gender":"female","race":"white","hair":"blonde","eye":"blue","face":"oval"},
            "Larisa":{"gender":"female","race":"white","hair":"brown","eye":"brown","face":"oval"},
            "Matej":{"gender":"male","race":"white","hair":"black","eye":"blue","face":"oval"},
            "Miha":{"gender":"male","race":"white","hair":"brown","eye":"green","face":"square"}}

现在我想比较两个嵌套的词典,找到DNA匹配值并用corrispondig DNA序列打印出名称及其特征。 我试过这个:

while True:

    for cat,car in dna_type.iteritems():
        for name,pers in suspects.iteritems():
            if car == pers:
                print suspects[name][car]

但我还在等输出。

1 个答案:

答案 0 :(得分:0)

您仍在等待输出的原因是:

  1. 您的for循环不会产生任何输出,因为测试car == pers永远不会为真。
  2. 但是,您可以通过指定for无限期地重复此while True循环。
  3. car == pers永远为真的原因是你期待car,其值类似

    {'brown': 'GCCAGTGCCG', 'blonde': 'TTAGCTATCGC', 'black': 'CCAGCAATCGC'}
    

    等于pers,其值为

    {'hair': 'blonde', 'gender': 'female', 'race': 'white', 'eye': 'blue', 'face': 'oval'}
    

    编辑以下问题

    数据定义:

    dna_type = {"hair":{"black":"CCAGCAATCGC","brown":"GCCAGTGCCG","blonde":"TTAGCTATCGC"},
                "face":{"square":"GCCACGG","round":"ACCACAA","oval":"AGGCCTCA"},
                "eye":{"blue":"TTGTGGTGGC","green":"GGGAGGTGGC","brown":"AAGTAGTGAC"},
                "gender":{"female":"TGAAGGACCTTC","male":"TGCAGGAACTTC"},
                "race":{"white":"AAAACCTCA","black":"CGACTACAG","asian":"CGCGGGCCG"}}
    
    suspects = {"Eva":{"gender":"female","race":"white","hair":"blonde","eye":"blue","face":"oval"},
                "Larisa":{"gender":"female","race":"white","hair":"brown","eye":"brown","face":"oval"},
                "Matej":{"gender":"male","race":"white","hair":"black","eye":"blue","face":"oval"},
                "Miha":{"gender":"male","race":"white","hair":"brown","eye":"green","face":"square"}}
    

    鉴于这些词典,下面的代码循环遍历suspects并从dna_type中选出匹配的DNA字符串:

    for suspect, characteristics in suspects.items():
        for characteristic,value in characteristics.items():
            print suspect, characteristic, value, dna_type[characteristic][value]
    

    产生以下输出,我认为它接近你想要的结果:

    Eva hair blonde TTAGCTATCGC
    Eva gender female TGAAGGACCTTC
    Eva race white AAAACCTCA
    Eva eye blue TTGTGGTGGC
    Eva face oval AGGCCTCA
    Matej hair black CCAGCAATCGC
    Matej gender male TGCAGGAACTTC
    Matej race white AAAACCTCA
    Matej eye blue TTGTGGTGGC
    Matej face oval AGGCCTCA
    Miha hair brown GCCAGTGCCG
    Miha gender male TGCAGGAACTTC
    Miha race white AAAACCTCA
    Miha eye green GGGAGGTGGC
    Miha face square GCCACGG
    Larisa hair brown GCCAGTGCCG
    Larisa gender female TGAAGGACCTTC
    Larisa race white AAAACCTCA
    Larisa eye brown AAGTAGTGAC
    Larisa face oval AGGCCTCA
    

    请注意,这绝对没有错误检查,因此如果dna_type中缺少数据(例如, face 的某些DNA字符串但缺少椭圆形面< / em>)它会失败。