如何检索密钥子串并按此子串计数?

2024-06-02 06:11:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下Python词典:

OrderedDict([('data(xxx_a1)_first_type', 0.12),
             ('data(xxx_a2)_first_type', 0.14),
             ('test(xx_b15)_second_type', 0.15)])

有没有办法计算first_typesecond_type,并计算每个类型的平均值?你知道吗

预期结果:

type         avg_val
first_type   0.13
second_type  0.15

Tags: testa2类型dataa1type词典xxx
3条回答

使用itertools.groupby假设数据是有序的。你知道吗

例如:

from collections import OrderedDict
from itertools import groupby

d = OrderedDict([('data(xxx_a1)_first_type', 0.12),
             ('data(xxx_a2)_first_type', 0.14),
             ('test(xx_b15)_second_type', 0.15)])

for k, v in groupby(d.items(), lambda x: "_".join(x[0].split("_")[-2:])):
    val = [i for _, i in v]
    print("{} {}".format(k, sum(val)/len(val)))

输出:

first_type 0.13
second_type 0.15

或者使用dict.setdefault

例如:

result = {}
for k, v in d.items():
    key = "_".join(k.split("_")[-2:])
    result.setdefault(key, []).append(v)

for k, v in result.items():
    print("{} {}".format(k, sum(v)/len(v)))
import pandas as pd
list_Tuples = [(z, np.mean([y for x,y in v.items() if x.endswith(z)]), len([y for x,y in v.items() if x.endswith(z)])) for z in ['first_type', 'second_type']]
pd.DataFrame(list_Tuples, columns=['type', 'avg_val', 'count'])

输出:

    type         avg_val  count
0   first_type   0.13     2
1   second_type  0.15     1

其中v是数据。你知道吗

假设只有两种类型(否则使用dict按类型存储列表):

from collections import OrderedDict
from statistics import mean

data = OrderedDict([('data(xxx_a1)_first_type', 0.12),
                    ('data(xxx_a2)_first_type', 0.14),
                    ('test(xx_b15)_second_type', 0.15)])


firsts = []
seconds = []
for key, value in data.items():
    if key.endswith("first_type"):
        firsts.append(value)
    else:
        seconds.append(value)

print("type", "avg_value", sep="\t\t")
print("first_type", mean(firsts), sep='\t')
print("second_type", mean(seconds), sep='\t')

相关问题 更多 >