Using dictionary key as a regular expression class

Chris Jones cjns1989 at gmail.com
Fri Jan 22 22:47:55 CET 2010


I was writing a script that counts occurrences of characters in source code files:

#!/usr/bin/python
import codecs
tcounters = {}
f = codecs.open('/home/gavron/git/screen/src/screen.c', 'r', "utf-8")
for uline in f:
  lline = []
  for char in uline[:-1]:
    lline += [char]
  counters = {}
  for i in set(lline):
    counters[i] = lline.count(i)
  for c in counters.keys():
    if c in tcounters:
      tcounters[c] += counters[c]
    else:
      tcounters.update({c: counters[c]})
  counters = {}
for c in tcounters.keys():
  print c, '\t', tcounters[c]

I was looking for a way to stat by finger actions on a US keyboard for a
traditional typist, and I was thinking I could use another dictionary
with keys that would be made up of the characters correponding to each
finger, such as '!1QqAaZz' for the left pinky, etc., hoping I would be
able to iterate the keys and match the currently processed character
with the key and increment it.

Is this something that makes sense, or should I look elsewhere?

Suggestions to improve the above snippet are also welcome.

Thanks,

CJ





More information about the Python-list mailing list