eh2deni
eh2deni

Reputation: 69

associative arrays awk vs python

I am using associative arrays in awk in order to perform calculations from values in a dataset. I wonder how to perform exactly the same operations in Python.

Input
Key1;cr;10
Key1;db;5
Key2;cr;5
Key2;db;7
Key2;cr;9
Key2;cr;12
Key3;db;3

awk -F";" '{a[$1]+=$2=="cr"?$3:$3*-1}END{for(i in a){print i FS a[i]}}' input

Output
Key1;5
Key2;19
Key3;9

Upvotes: 0

Views: 289

Answers (1)

furas
furas

Reputation: 142869

Python 2:

data = '''Key1;cr;10
Key1;db;5
Key2;cr;5
Key2;db;7
Key2;cr;9
Key2;cr;12
Key3;db;3
'''

results = {}

for line in data.splitlines():

    fields = line.split(';')

    if fields[0] not in results:
        results[fields[0]] = 0

    if fields[1] == 'cr':
        results[fields[0]] += int(fields[2])
    else:
        results[fields[0]] -= int(fields[2])

#--- END ---

for key in sorted(results.keys()):
    print '%s;%s' % (key, results[key])

EDIT:

By the way:

a[$1]+=$2=="cr"?$3:$3*-1

can be done in Python (pseudocode)

a[$1] += ( $3 if $2=="cr" else $3*-1 )

Upvotes: 1

Related Questions