wilberox
wilberox

Reputation: 193

python making list of averages from dictionary of dictionaries: all averages same

I have a dictionary of dictionaries, d:

d = {'redFish': {'redFish': 'inf', 'blueFish': 9, 'twoFish': 10, 'oneFish': 6},
'blueFish': {'redFish': 9, 'blueFish': 'inf', 'twoFish': 11, 'oneFish': 10},
'twoFish': {'redFish': 10, 'blueFish': 11, 'twoFish': 'inf', 'oneFish': 8},
'oneFish': {'redFish': 6, 'blueFish': 10, 'twoFish': 8, 'oneFish': 'inf'}}

I have a function that finds and returns the key-key-value pair with the lowest value:

lowestPair = ['name1', 'name2', float('inf')]
for name1 in d.keys():
    for name2 in d[name1].keys():
        if d[name1][name2] < lowestPair[2]:
            lowestPair = [name1, name2, d[name1][name2]]

My lowest pair acts as a cluster that will be treated as one entity. I am now trying to go through my dictionary of dictionaries, and for each species find the values which are the average between the species I am looking at, and both the species in my new cluster.

i.e. as redFish and oneFish are the species in lowest Pair, I want to find the average between redFish blueFish and oneFish Bluefish, and also the average between oneFish twoFish and oneFish blueFish.

I have a piece of code that does this:

averageList = []
for name in lowestPair[0:2]:
    for otherName in d[name].keys():
        if otherName not in lowestPair:
            average = (d[lowestPair[0]][otherName] + d[lowestPair[1]][otherName])/2
            nameDict[name][otherName] = average 
            averageList.append(average) 

However, the average list returns as [9, 9, 9, 9] which is not the correct answer, as the average for redFish blueFish and oneFish Bluefish, and the average between oneFish twoFish and redFish twoFish should be 9 and 9.5. What am I doing wrong?

Upvotes: 0

Views: 76

Answers (1)

Jason K Lai
Jason K Lai

Reputation: 1540

Using the code with a couple of minor calculations, I was able to get [9.5, 9.0, 9.75, 8.5]

The two changes I made was that I had to cast the output of dictionary d to a float(), and I changed nameDict to d, since nameDict was not defined in the example code provided. Here is the resulting code below:

d = {'redFish': {'redFish': 'inf', 'blueFish': 9, 'twoFish': 10, 'oneFish': 6}, 'blueFish': {'redFish': 9, 'blueFish': 'inf', 'twoFish': 11, 'oneFish': 10}, 'twoFish': {'redFish': 10, 'blueFish': 11, 'twoFish': 'inf', 'oneFish': 8}, 'oneFish': {'redFish': 6, 'blueFish': 10, 'twoFish': 8, 'oneFish': 'inf'}}

lowestPair = ['name1', 'name2', float('inf')]
for name1 in d.keys():
    for name2 in d[name1].keys():
        if float( d[name1][name2] ) < lowestPair[2]:
            lowestPair = [name1, name2, d[name1][name2]]

averageList = []
for name in lowestPair[0:2]:
    for otherName in d[name].keys():
        if otherName not in lowestPair:
            average = (float( d[lowestPair[0]][otherName] ) + float( d[lowestPair[1]][otherName] ))/2
            d[name][otherName] = average 
            averageList.append(average) 

print( averageList )

Upvotes: 1

Related Questions