Using dictionary key as a regular expression class

C

Chris Jones

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 = 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
 
A

Arnaud Delobelle

Chris Jones said:
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 = 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)
 
C

Chris Jones

On Fri, Jan 22, 2010 at 05:07:13PM EST, Arnaud Delobelle wrote:

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

Ah, yes.. much better - I grew suspicious of my 'effort' when I realized
I could have written a shorter version in C. :)

CJ
 
S

Steve Holden

Chris said:
On Fri, Jan 22, 2010 at 05:07:13PM EST, Arnaud Delobelle wrote:

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

Ah, yes.. much better - I grew suspicious of my 'effort' when I realized
I could have written a shorter version in C. :)
Congratulations. That perception shows a sound appreciation of Python's
design.

regards
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top