counting character occurrences

Wilbert Berendsen info at wilbertberendsen.nl
Sat Jan 23 13:44:00 EST 2010


Op vrijdag 22 januari 2010 schreef Arnaud:

> 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)

Or using Counter from Python3.1 collections:

import codecs
from collections import Counter

filename = '/home/gavron/git/screen/src/screen.c'
with codecs.open(filename, 'r', 'utf-8') as f:
	counted = Counter(f.read())

for c, n in counted:
	print(c, n, sep='\t')
	

with best regards,
Wilbert Berendsen

-- 
http://www.wilbertberendsen.nl/



More information about the Python-list mailing list