在字典列表中找到按类别划分的平均值(非熊猫解决方案)

时间:2018-04-23 10:16:09

标签: python list mean

我试图找到一个非大熊猫解决方案来按类别进行总结。

我将此查找表作为dicts列表:

lookup_table = [
{"urban_rural": "urban", "technology": "FTTC", "speed": 50},
{"urban_rural": "rural", "technology": "FTTC", "speed": 10},
{"urban_rural": "urban", "technology": "FTTC", "speed": 30}
]

我想找到速度'的平均值。按类别(' urban_rural'和'技术')所以我最终得到这个:

lookup_table_mean_values = [
{"urban_rural": "urban", "technology": "FTTC", "speed": 40},
{"urban_rural": "rural", "technology": "FTTC", "speed": 10}
]

修改(添加当前代码):

我不想让水变得浑浊,但正如@Patrick Artner所要求的那样,这就是我要去的地方。目前this question为dict提供了建议的答案,同时提供了简单循环和Iteritems选项,但是到目前为止我还没有能够适应dict结构列表。

我会很高兴使用这样的东西:

lookup_table_mean_values =[float(sum(values)) / len(values) for key, values in lookup_table .iteritems()]

2 个答案:

答案 0 :(得分:2)

lookup_table = [
  {"urban_rural": "urban", "technology": "FTTC", "speed": 50},
  {"urban_rural": "rural", "technology": "FTTC", "speed": 10},
  {"urban_rural": "urban", "technology": "FTTC", "speed": 30}
]

dic = {}
for d in lookup_table:
    key = d['urban_rural'], d['technology']
    if key not in dic: dic[key] = []
    dic[key].append(d['speed'])

mean = [{"urban_rural":key[0], "technology":key[1], "speed":sum(val)/len(val)}
         for key,val in dic.items()]
print(mean)

输出:

[{'urban_rural': 'urban', 'technology': 'FTTC', 'speed': 40.0}, {'urban_rural': 'rural', 'technology': 'FTTC', 'speed': 10.0}]

答案 1 :(得分:0)

如下:

lookup_table = [
{"urban_rural": "urban", "technology": "FTTC", "speed": 50},
{"urban_rural": "rural", "technology": "FTTC", "speed": 10},
{"urban_rural": "urban", "technology": "FTTC", "speed": 30},
]


def get_mean(dict, by_mean):
    mean = 0
    for j in range(0, len(dict)):
        mean += dict[j][by_mean]

    mean = mean / len(dict)
    return mean


def foo(dict, key, value, by_mean):
    temp1 = []
    temp2 = []
    res = []
    for i in range(0, len(dict)):
        if dict[i][key] == value:
            temp1.append(dict[i])
    else:
        temp2.append(dict[i])

    res.append(temp1[0])
    res.append(temp2[0])
    res[0][by_mean] = get_mean(temp1, by_mean)
    res[1][by_mean] = get_mean(temp2, by_mean)

    return res


print foo(lookup_table, 'urban_rural', 'urban', 'speed')

出:

[{'speed': 40, 'technology': 'FTTC', 'urban_rural': 'urban'}, {'speed': 10, 'technology': 'FTTC', 'urban_rural': 'rural'}]