klim@moose:~> python Python 2.5.2 (r252:60911, Mar 1 2008, 13:52:45) [GCC 4.2.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = {} <----- an empty dictionary >>> a['klim'] = 100 <----- (key,value) = ('klim', 100) is added >>> a['oak'] = 20 <----- another pair is added >>> for k in a.keys(): <----- iterates through all keys ... print k ... klim oak >>> a['klim'] += 200 <----- update the value >>> print a['klim'] 300 >>> klim@moose:~> cat sum-vars.py #!/usr/bin/python import sys sum = {} <--- initialze the dictionary for l in sys.stdin.readlines(): <--- for each line read from stdin if l[0] == '#': continue <--- skip a comment line fs = l[:-1].split() <--- remove \n and split the line into fields if fs[0] not in sum: <--- if key not present sum[fs[0]] = 0 <--- add a new pair for n in fs[1:] : <--- for other fields (i.e numbers) in line sum[fs[0]] += int(n) <--- summing for k in sum.keys(): print k, sum[k] klim@moose:~> cat IN-num klim 233 112 33 <----- input data begins # oh!, just a test <-- comment # again <-- comment milk 333 22 <-- klim 999 22 <-- oak 222 <-- redcow 1023 <-- oak 11 <----- input data ends here klim@moose:~/demo-awk> python sum-vars.py < IN-num redcow 1023 <------ the answer is ...... klim 1399 oak 233 milk 355 klim@moose:~>