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


Groups > comp.lang.python > #2476 > unrolled thread

better way to do this in python

Started byMag Gam <magawake@gmail.com>
First post2011-04-02 19:58 -0400
Last post2011-04-02 19:58 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python


Contents

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

#2476 — better way to do this in python

FromMag Gam <magawake@gmail.com>
Date2011-04-02 19:58 -0400
Subjectbetter 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?

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web