better way to do this in python

M

Mag Gam

I have a file like this,


cat file
aaa
bbb
aaa
aaa
aaa

awk '{x[$1]++}END { for (i in x) {print i,x} } ' test
bbb 1
aaa 4


I suppose I can do something like this.
(pseudocode)

d={}
try:
d[key]+=1
except KeyError:
d[key]=1


I was wondering if there is a pythonic way of doing this? I plan on
doing this many times for various files. Would the python collections
class be sufficient?
 

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
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top