Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2476 > unrolled thread
| Started by | Mag Gam <magawake@gmail.com> |
|---|---|
| First post | 2011-04-02 19:58 -0400 |
| Last post | 2011-04-02 19:58 -0400 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
better way to do this in python Mag Gam <magawake@gmail.com> - 2011-04-02 19:58 -0400
| From | Mag Gam <magawake@gmail.com> |
|---|---|
| Date | 2011-04-02 19:58 -0400 |
| Subject | better way to do this in python |
| Message-ID | <mailman.141.1301788692.2990.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web