KJ9
KJ9

Reputation: 191

How to sum value in list using some key?

I have just begun studying Python. I want sum some list data in 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

Script

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)

I want to sum like a dictionary.

But I need below output.

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

Upvotes: 2

Views: 97

Answers (3)

Stephen Rauch
Stephen Rauch

Reputation: 49784

With a dict, and setdefault() you can do:

Before the loop:

data_dict = {}

In the loop:

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

Result:

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

Test Code:

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

Upvotes: 0

bubble
bubble

Reputation: 1672

I would suggest you to use Pandas package for such kind of data evaluations.

If the data is stored in the file 'data.csv', you can do data processing as follows:

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

Upvotes: 0

cdlane
cdlane

Reputation: 41872

I believe this problem cries out for a compound (two level) 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=',')

OUTPUT

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

Upvotes: 1

Related Questions