Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2476

better way to do this in python

Date 2011-04-02 19:58 -0400
Subject better way to do this in python
From Mag Gam <magawake@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.141.1301788692.2990.python-list@python.org> (permalink)

Show all headers | View raw


I have a file like this,


cat file
aaa
bbb
aaa
aaa
aaa

awk '{x[$1]++}END { for (i in x) {print i,x[i]} } ' 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?

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

better way to do this in python Mag Gam <magawake@gmail.com> - 2011-04-02 19:58 -0400

csiph-web