0

i'm new at Python and i'm tryng to find a solution to thi problem. I have this two nested dictionaries:`

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"}}

Now i want to compare the two nested dictionaries , finding the DNA matching values and print out the name and its features with the corrispondig DNA sequence. I've tried this:

while True:

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

but i'm still waiting for an output.

donkopotamus
  • 22,114
  • 2
  • 48
  • 60
  • It's not at all clear here what it means to "compare" the dictionaries, or what the rules should be for a "match". But this surely isn't a duplicate of any JSON question, and ceratinly not one aimed at extracting a single value - the code is clearly trying to iterate over data. Un-duped, and voted to close again. – Karl Knechtel Jul 02 '22 at 02:26

1 Answers1

0

The reason you are still waiting for output is:

  1. Your for loop produces no output because the test car == pers is never True.
  2. But you repeat this for loop indefinitely by specifying while True.

And the reason car == pers is never True is you are expecting car, which has a value like

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

to be equal to pers, which has a value like

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

Edit following clarification of question

Data definition:

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"}}

Given these dictionaries, the code below loops through suspects and picks out the matching DNA string from dna_type:

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

That produces the following output, which I think is close to what you want:

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

Do be aware that this has absolutely no error checking, so if there is missing data in dna_type (for example, some DNA strings for face but missing oval face) it will fail.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • Thanks for the explanation, what i mean is that: i want to compare the two nested dict and when there is a match between for example the Brown DNA code and the Brown feature of a suspect, just to print out the name of the suspect with hair:DNA , gender:DNA, race:DNA and so on... – Luigi Sansone Jun 18 '17 at 12:45
  • I've tried your solution but it gives me an error: Traceback (most recent call last): File "C:/Python27/forens.py", line 16, in print suspect, characteristic, value, dna_type[characteristic][value] TypeError: unhashable type: 'dict' – Luigi Sansone Jun 18 '17 at 13:13
  • That message indicates we are not working with the same data. It says you have a variable, which must be either `characteristic` or `data`, that is a `dict` when it should be a string. I have retried the exercise and on my side it still works. Start a fresh Python session and try again, complete with definition of `dna_type` and `suspects`, as shown in the edited response. – BoarGules Jun 18 '17 at 21:32