Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #35148
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'none,': 0.05; 'processed': 0.05; 'subject:code': 0.07; 'subject:file': 0.07; 'dict': 0.09; 'long)': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:same': 0.09; 'dec': 0.15; '":"': 0.16; "'r')": 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'simplified': 0.16; 'subject:txt': 0.16; 'wed,': 0.16; 'skip:= 10': 0.20; 'bit': 0.21; 'list:': 0.27; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; 'indentation': 0.29; 'words': 0.29; '"the': 0.29; 'subject:last': 0.30; 'gets': 0.32; 'could': 0.32; 'print': 0.32; 'url:home': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'open': 0.35; 'continue': 0.35; 'next': 0.35; 'received:org': 0.36; 'charset:us-ascii': 0.36; 'previous': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'first': 0.61; 'subject:...': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.63; 'taking': 0.65; 'dennis': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
| Subject | Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file |
| Date | Wed, 19 Dec 2012 13:29:00 -0500 |
| Organization | > Bestiaria Support Staff < |
| References | <f91585d2-ca8d-4b01-96a0-db817c419858@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | adsl-76-249-24-60.dsl.klmzmi.sbcglobal.net |
| X-Newsreader | Forte Agent 3.3/32.846 |
| X-No-Archive | YES |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.1069.1355941724.29569.python-list@python.org> (permalink) |
| Lines | 48 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1355941724 news.xs4all.nl 6981 [2001:888:2000:d::a6]:54558 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:35148 |
Show key headers only | View raw
On Wed, 19 Dec 2012 02:45:13 -0800 (PST), dgcosgrave@gmail.com declaimed
the following in gmane.comp.python.general:
> tm =open('ask.txt', 'r')
> dict = {}
> for line in tm:
> line = line.strip()
> line = line.translate(None, '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')
> line = line.lower()
> list = line.split(' ')
You could (though it gets a bit long) combine some of the above...
list = line.strip().translate(
None,
"the punctuation set"
).lower().split()
# taking advantage that open ()/[]/{} automatically continue on next
lines
> for word in list:
INDENTATION! As coded, you first do the strip/translate/lower/split
on EACH line of the file... THEN you are processing the words in the
LAST line processed in the previous loop.
> if word in dict:
> count = dict[word]
> count += 1
> dict[word] = count
> else:
> dict[word] = 1
More indentation -- I suspect your want the else: and following line
to be indented the same as the if line...
Though the whole block can be simplified to
dict[word] = dict.get(word, 0) + 1
> for word, count in dict.iteritems():
> print word + ":" + str(count)
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file dgcosgrave@gmail.com - 2012-12-19 02:45 -0800
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-12-19 12:55 +0200
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file dgcosgrave@gmail.com - 2012-12-19 03:28 -0800
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-19 11:03 +0000
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file dgcosgrave@gmail.com - 2012-12-19 03:34 -0800
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file Thomas Bach <thbach@students.uni-mainz.de> - 2012-12-19 12:21 +0100
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file dgcosgrave@gmail.com - 2012-12-19 03:37 -0800
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file dgcosgrave@gmail.com - 2012-12-19 03:37 -0800
Re: counting how often the same word appears in a txt file...But my code only prints the last line entry in the txt file Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-12-19 13:29 -0500
csiph-web