Using dictionary key as a regular expression class

Arnaud Delobelle arnodel at googlemail.com
Fri Jan 22 23:07:13 CET 2010


Chris Jones <cjns1989 at gmail.com> writes:

> 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

Why not just start with (untested):

import codecs
from collections import defaultdict

tcounters = defaultdict(int)
f = codecs.open('/home/gavron/git/screen/src/screen.c', 'r', "utf-8")

for c in f.read():
    tcounters[c] += 1

for c, n in tcounters.iteritems():
    print "%r\t%i" % (c, n)

-- 
Arnaud



More information about the Python-list mailing list