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


Groups > comp.lang.python > #2476

better way to do this in python

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!news2.arglkargh.de!news.wiretrip.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <magawake@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.015
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'suppose': 0.05; 'python': 0.07; 'collections': 0.09; 'received:209.85.160.174': 0.09; 'received:mail-gy0-f174.google.com': 0.09; 'files.': 0.10; 'subject:python': 0.15; 'awk': 0.16; 'keyerror:': 0.16; 'pythonic': 0.16; 'this?': 0.18; 'wondering': 0.19; 'message- id:@mail.gmail.com': 0.28; 'received:209.85.160': 0.28; 'class': 0.29; 'this.': 0.30; "skip:' 10": 0.32; 'to:addr:python-list': 0.32; 'test': 0.33; 'there': 0.35; 'file': 0.35; 'try:': 0.35; 'doing': 0.36; 'received:209.85': 0.37; 'received:google.com': 0.38; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'except': 0.39; 'would': 0.40; 'header:Received:5': 0.40; 'subject:this': 0.79; 'aaa': 0.84; 'sufficient?': 0.84; 'subject:better': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=S+S8Y34AkKHb61XtbIqsxfU4aADoVYTqcD9HMDh10BY=; b=vW9ZVIwInZVH0M044OPHrxeqC7V1R34X70KjvYZRd/MddW5bcYHfWkpFrCTlL0qq2t p85pEOC1sfI5xpQwz3mZNeXo8XgFcab1wt6yjR99u+WcI7BNPVFbTAHYpP903w2qhqev jmC8nSSnZxNgQP9L39nhEBaQYUEDDkXvov9I4=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=oiLseRJXRozNmbvSM3CDt9L2ybcQ0zdf5++XlNp1YWlV+KqP0x2YMl6MCB4aXP022C WfGc7bK0c+K16bQteRM2YaiHplSR7+oNJDCjxRT+P93wJlE7bmt27JTEE0e9GDW2YOGi jyS21tWVyymW9laV0d+pJJG65O8JwJfydnRjw=
MIME-Version 1.0
Date Sat, 2 Apr 2011 19:58:09 -0400
Subject better way to do this in python
From Mag Gam <magawake@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.141.1301788692.2990.python-list@python.org> (permalink)
Lines 28
NNTP-Posting-Host 82.94.164.166
X-Trace 1301788692 news.xs4all.nl 81484 [::ffff:82.94.164.166]:57706
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:2476

Show key headers only | 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