Compare values of two nested dictionaries

时间:2019-05-27 08:44:44

标签: python dictionary

I have two nested dictionaries data_annot (example):

path/r1.png:
  Center_Coordinates:
  - 2680.0
  - 89.0
  Points: '[(4, 2655), (174, 2655), (4, 2705), (174, 2705)]'
  Text: IESPID

and data (example):

path/cr1.png:
  Center_Coordinates:
  - 186.0
  - 1101.0
  Points: '[(545, 67), (1657, 67), (545, 305), (1657, 305)]'
  Text: Subject Number 18F-A 

I need to apply pythagorean theorem to Center_Coordinates of two dictionaries and find the minimal possible number for each case. That is to say take the Center_Coordinates of data_annot and find the minimum of sqrt[(X1-X2)^2 + (Y1-Y2)^2] from each Center_Coordinates of data.

I have tried the enclosed code, but it obviously doesn't work and I am new to python and need help with this.

for key, value in data_annot.items():

    for nested_key in value:
        if  "Center_Coordinates" in nested_key: 
            coord_annot.append(value[nested_key])

for key, value in data.items():

    for nested_key in value:
        for i in range (len(coord_annot)):
            if  "Center_Coordinates" in nested_key:
                pyutagoras.append((value[nested_key][0] - coord_annot[i][0])**2 + (value[nested_key][1] - coord_annot[i][1])**2)
        mins.append(min(pyutagoras))

And the expected result is like the min(pyutagoras) for each r*.png from cr*.png. Say the distance is minimal for r1.png from cr10.png

0 个答案:

没有答案