如何使用某个键对列表中的值求和?

时间:2017-07-21 03:00:11

标签: python list dictionary sum

我刚开始学习Python。 我想在Python中总结一些列表数据。

convert.txt。

/vol/vol0/,1GB,1GB,1GB,7%,FAS8040-ZZZZ
/vol/vol0/,466GB,31GB,435GB,7%,FAS8040-ZZZZ
/vol/vol0/,1GB,1GB,1GB,8%,FAS8040-CCCC
/vol/vol0/,466GB,38GB,428GB,8%,FAS8040-CCCC
/vol/vol0/,200GB,200GB,200GB,7%,FAS8040-XXXX
/vol/vol0/,100GB,100GB,100GB,7%,FAS8040-YYYY

脚本

with open("convert.txt", "r") as f:
    for line in f:
            line = line.split(',')
            vser = line[5].replace('\n','')
            tcap = int(line[1].replace('GB',''))
            ucap = int(line[2].replace('GB',''))
            acap = int(line[3].replace('GB',''))
            data = vser, tcap, ucap, acap
            print (data)

我想像字典一样总结。

但我需要低于输出。

FAS8040-ZZZZ,467,32,436
FAS8040-CCCC,467,39,429
FAS8040-XXXX,200.200.200
FAS8040-YYYY,100,100,100

3 个答案:

答案 0 :(得分:1)

我相信这个问题需要一个复合(两级)(id, email)

defaultdict

<强>输出

from collections import defaultdict

volumes = defaultdict(lambda: defaultdict(int))

with open("convert.txt") as handle:

    for line in handle:
        _, tcap, ucap, acap, _, vser = line.rstrip().split(',')

        volumes[vser]['tcap'] += int(tcap.replace('GB', ''))
        volumes[vser]['ucap'] += int(ucap.replace('GB', ''))
        volumes[vser]['acap'] += int(acap.replace('GB', ''))

for volume, capacities in volumes.items():
    print(volume, *[capacities[cap] for cap in ['tcap', 'ucap', 'acap']], sep=',')

答案 1 :(得分:0)

使用dict,setdefault()可以执行:

循环之前:

data_dict = {}

在循环中:

data_line = data_dict.setdefault(vser, [0, 0, 0])
data_line[0] += tcap
data_line[1] += ucap
data_line[2] += acap

结果:

for key, data in data_dict.items():
    print(','.join([key] + [str(d) for d in data]))

FAS8040-YYYY,100,100,100
FAS8040-ZZZZ,467,32,436
FAS8040-XXXX,200,200,200
FAS8040-CCCC,467,39,429

测试代码:

data_str = """
    /vol/vol0/,1GB,1GB,1GB,7%,FAS8040-ZZZZ
    /vol/vol0/,466GB,31GB,435GB,7%,FAS8040-ZZZZ
    /vol/vol0/,1GB,1GB,1GB,8%,FAS8040-CCCC
    /vol/vol0/,466GB,38GB,428GB,8%,FAS8040-CCCC
    /vol/vol0/,200GB,200GB,200GB,7%,FAS8040-XXXX
    /vol/vol0/,100GB,100GB,100GB,7%,FAS8040-YYYY
""".split('\n')[1:-1]

data_dict = {}
for line in data_str:
    line = line.split(',')
    vser = line[5].replace('\n', '')
    tcap = int(line[1].replace('GB', ''))
    ucap = int(line[2].replace('GB', ''))
    acap = int(line[3].replace('GB', ''))
    line = vser, tcap, ucap, acap
    data_line = data_dict.setdefault(vser, [0, 0, 0])
    data_line[0] += tcap
    data_line[1] += ucap
    data_line[2] += acap

for key, data in data_dict.items():
    print(','.join([key] + [str(d) for d in data]))

答案 2 :(得分:0)

我建议您使用Pandas软件包进行此类数据评估。

如果数据存储在'data.csv'文件中,您可以按如下方式进行数据处理:

import pandas as pd
df = pd.read_csv('data.csv', header=None)
df[[1,2,3]] = df.replace(to_replace='GB', value='', regex=True)[[1,2,3]].apply(pd.to_numeric)
df.groupby([5,0]).sum()
# write to csv if needed... df.to_csv