Skip to content Skip to sidebar Skip to footer

Evaluating Values Within A Dictionary For Different Keys

I have a nested dictionary to which I would like to obtain the differences between each value. locsOne = {1:[100], 2:[200], 3:[250]} (these values here are just examples) I'm atte

Solution 1:

locsOne = {1:[100], 2:[200], 3:[250]}
locsTwo = {}
for k1 in locsOne:
    v1 = locsOne[k1][0]
    locsTwo[k1] = {k2: abs(v1 - locsOne[k2][0]) for k2 in locsOne
                   if k1 != k2 and abs(v1 - locsOne[k2][0]) <= 450}
print(locsTwo)

output:

{1: {2: 100, 3: 150}, 2: {1: 100, 3: 50}, 3: {1: 150, 2: 50}}

Post a Comment for "Evaluating Values Within A Dictionary For Different Keys"