python - 我需要 python 中 list_of_dicts_of_lists 的按列中位数

标签 python list dictionary nested median

我有这个数据:

list_of_dicts_of_lists = [
    {'a': [1,2], 'b': [3,4], 'c': [3,2], 'd': [2,5]}
    {'a': [2,2], 'b': [2,2], 'c': [1,6], 'd': [4,7]}
    {'a': [2,2], 'b': [5,2], 'c': [3,2], 'd': [2,2]}
    {'a': [1,2], 'b': [3,4], 'c': [1,6], 'd': [5,5]} 
    ]

我需要这个结果:

median_dict_of_lists = (
    {'a': [1.5,2], 'b': [3,3], 'c': [2,4], 'd': [3,5]}
    )

...其中每个值都是上面相应列的中位数。

我需要可用的模式字典和不存在模式时的中值字典。我能够通过将每个字典串起来,获取字符串列表的模式,然后 ast.literal_eval(most_common_string) 返回字典来快速而肮脏的 statistics.mode() ,但在没有模式的情况下,我需要一个按列的中位数。

我知道如何使用 statistics.median();然而,将它应用于这种情况的嵌套符号,按列排列,让我感到困惑。

数据都是 float ;我把它写成 int 只是为了更容易阅读。

最佳答案

您可以将 statistics.medianitertools.groupby 一起使用:

import statistics
import itertools
list_of_dicts_of_lists = [
  {'a': [1,2], 'b': [3,4], 'c': [3,2], 'd': [2,5]},
  {'a': [2,2], 'b': [2,2], 'c': [1,6], 'd': [4,7]},
  {'a': [2,2], 'b': [5,2], 'c': [3,2], 'd': [2,2]},
  {'a': [1,2], 'b': [3,4], 'c': [1,6], 'd': [5,5]} 
]
new_listing = [(a, list(b)) for a, b in itertools.groupby(sorted(itertools.chain(*map(lambda x:x.items(), list_of_dicts_of_lists)), key=lambda x:x[0]), key=lambda x:x[0])]
d = {a:zip(*map(lambda x:x[-1], b)) for a, b in new_listing}
last_data = ({a:[statistics.median(b), statistics.median(c)] for a, [b, c] in d.items()},)

输出:

({'a': [1.5, 2.0], 'b': [3.0, 3.0], 'c': [2.0, 4.0], 'd': [3.0, 5.0]},)

关于python - 我需要 python 中 list_of_dicts_of_lists 的按列中位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49353986/

相关文章:

python - python dict 的键默认排序发生了什么

java - 如何使用另一个列表中的 map 属性创建新列表

python - Python2.6 中 ElementTree 的 iter() 等价物

python - 使用 Python 删除 IP 地址中的前导零

python - Python 中有没有内置的方法来检查单个列表索引

c++ - 链表中的节点可以有指向自身的指针吗? C++

python - 这个函数定义在 python 中如何工作?

c# - 如何复制 BlockingCollection 并编辑新的而不编辑源

list - 改进我所有功能的 Haskell 代码

python - 将默认时间添加到字典