简体   繁体   English

如何检索关键子字符串并以此子字符串计数?

[英]How to retrieve key substring and count by this substring?

I have the following dictionary in Python: 我在Python中有以下字典:

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

Is there any way to count first_type and second_type , and calculate the average value per type? 有什么方法可以计算first_typesecond_type ,并计算每种类型的平均值?

The expected result: 预期结果:

type         avg_val
first_type   0.13
second_type  0.15
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'])

Output: 输出:

    type         avg_val  count
0   first_type   0.13     2
1   second_type  0.15     1

where v is the data. 其中v是数据。

Assuming there are only two types (otherwise use a dict to store the lists by type) : 假设只有两种类型(否则使用字典按类型存储列表):

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')

Using itertools.groupby assuming the data is ordered. 通过假定数据已排序来使用itertools.groupby

Ex: 例如:

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)))

Output: 输出:

first_type 0.13
second_type 0.15

Or using dict.setdefault 或者使用dict.setdefault

Ex: 例如:

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)))

You can use a collections.defaultdict to group the values, then apply statistics.mean to get the average: 您可以使用collections.defaultdict将值分组,然后应用statistics.mean获得平均值:

from collections import defaultdict
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)])

d = defaultdict(list)
for k, v in data.items():
    *_, key = k.split('_', 2)
    d[key].append(v)

for k, v in d.items():
    print('%s %.2f' % (k, mean(v)))

Output: 输出:

first_type 0.13
second_type 0.15

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

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