简体   繁体   English

python从字典中制作平均值列表:所有平均值相同

[英]python making list of averages from dictionary of dictionaries: all averages same

I have a dictionary of dictionaries, d :我有一本字典, 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.我现在正在尝试浏览我的字典,并为每个物种找到我正在查看的物种与新集群中的物种之间的平均值的值。

ie 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.即由于 redFish 和 oneFish 是最低 Pair 中的物种,我想找到 redFish blueFish 和 oneFish Bluefish 之间的平均值,以及 oneFish twoFish 和 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 .然而,平均列表返回为[9, 9, 9, 9]这不是正确答案,作为 redFish blueFish 和 oneFish Bluefish 的平均值,并且 oneFish twoFish 和 redFish twoFish 之间的平均值应该是99.5 What am I doing wrong?我究竟做错了什么?

Using the code with a couple of minor calculations, I was able to get [9.5, 9.0, 9.75, 8.5]使用带有几个小计算的代码,我能够得到[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.我所做的两个更改是我必须将字典d的输出转换为float() ,并且我将nameDict更改为d ,因为nameDict未在提供的示例代码中定义。 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 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM