Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7946
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <chris@rebertia.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.006 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; '#if': 0.07; 'function,': 0.07; 'argument,': 0.09; 'dict': 0.09; 'mailinglist': 0.09; 'pm,': 0.10; 'output': 0.11; 'def': 0.12; 'wrote:': 0.14; 'computes': 0.16; 'count)': 0.16; 'file;': 0.16; 'filename,': 0.16; 'lengths': 0.16; 'received:209.85.213.174': 0.16; 'received:mail- yx0-f174.google.com': 0.16; 'subject:print': 0.16; '\xc2\xa0if': 0.16; 'argument': 0.16; 'cc:addr:python-list': 0.17; 'cheers,': 0.19; 'header:In-Reply-To:1': 0.21; 'loop': 0.22; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'code': 0.24; 'function': 0.25; 'statement': 0.26; 'tried': 0.27; 'message-id:@mail.gmail.com': 0.28; 'thanks': 0.28; 'sat,': 0.29; "python's": 0.29; 'least': 0.30; 'cc:addr:python.org': 0.30; 'print': 0.31; 'headers': 0.32; 'posts': 0.32; 'word.': 0.32; 'words,': 0.32; 'skip:" 20': 0.33; 'lines': 0.33; 'chris': 0.34; 'showing': 0.34; 'skip:" 10': 0.35; 'body,': 0.35; 'latter': 0.35; 'numbers.': 0.35; 'skip:o 20': 0.37; 'table': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.37; 'received:209.85.213': 0.37; 'run': 0.38; 'word,': 0.38; 'subject:: ': 0.38; 'skip:s 20': 0.39; 'should': 0.39; 'said': 0.39; 'received:209': 0.39; 'level.': 0.39; 'current': 0.40; 'header': 0.40; 'really': 0.40; 'within': 0.60; 'your': 0.60; 'direction.': 0.65; 'skip:\xc2 10': 0.72; 'reply,': 0.73; 'future,': 0.76; 'filename:': 0.84; 'irrelevant': 0.84; 'sender:addr:chris': 0.84; '\xc2\xa0it': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=cVx3vZuspJCcApPUSE9puXTCqagnWggU9NHQoHltvzY=; b=A67ngAkfLUV3hvzW3dOZGBat7C99AUiWve9jKrb68SJoSs5nFD6f+9ZXWmj131yRJf IDqzFq/aGTQM/xYy/prny4RuYlPQeNEHkK5ebtPo3h0Xg2dj66LmDqjXZvXvla80fEx1 LiFl5nj3DFbBV+vFm8oYZXpALSqKNcQLpCtXA= |
| DomainKey-Signature | a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=NCRFfGNCevZCclI8hSfbPCpyPxYAb1zIrT/6DFMHeHR7v2B+h6RjzH+Uv2KT29Gj/u ibCBbAmwMy7cwmwVSEDG7my4O+8/6Gm0oQDRv/axjWVEmqj8K+AmmJB0xtaWjVKazE4k PEbjFJquY3wgvO5agtYVIzzR/AaDK8VdPCvnQ= |
| MIME-Version | 1.0 |
| Sender | chris@rebertia.com |
| In-Reply-To | <BANLkTinhEnhnrrxqtumFhSG7nEgN6cP3Jg@mail.gmail.com> |
| References | <BANLkTinhEnhnrrxqtumFhSG7nEgN6cP3Jg@mail.gmail.com> |
| Date | Sat, 18 Jun 2011 23:43:07 -0700 |
| X-Google-Sender-Auth | bYn_PfdERsLZGxY-TlwKOtiOAlI |
| Subject | Re: print header for output |
| From | Chris Rebert <clp2@rebertia.com> |
| To | Cathy James <nambo4jb@gmail.com> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | python-list@python.org |
| 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.1308465791.1164.python-list@python.org> (permalink) |
| Lines | 59 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1308465791 news.xs4all.nl 49045 [::ffff:82.94.164.166]:56197 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:7946 |
Show key headers only | View raw
On Sat, Jun 18, 2011 at 9:57 PM, Cathy James <nambo4jb@gmail.com> wrote:
> I managed to get output for my function, thanks much for your
> direction. I really appreciate the hints. Now I have tried to place
> the statement "print ("Length \t" + "Count\n")" in different places in
> my code so that the function can print the headers only one time in
> this manner:
>
> Count Length
> 4 7
> 8 1
> 12 2
>
>
> Code so far:
> def fileProcess(filename = open('declaration.txt', 'r')):
>
> """Call the program with an argument,
> it should treat the argument as a filename,
> splitting it up into words, and computes the length of each word.
> print a table showing the word count for each of the word lengths
> that has been encountered."""
>
> freq = {} #empty dict to accumulate word count and word length
> print ("Length \t" + "Count\n")
> for line in filename:
> punc = string.punctuation + string.whitespace#use Python's
> built-in punctuation and whiitespace
> for word in (line.replace (punc, "").lower().split()):
> if word in freq:
> freq[word] +=1 #increment current count if word already in dict
>
> else:
> freq[word] = 1 #if punctuation encountered,
> frequency=0 word length = 0
Un-indent the following 3 lines by 1 level. As I said in my prior
reply, you want the output code to run once per call to your function,
not once per line in the file; since you have said lines within the
`for line in filename` loop body, the latter is currently happening.
Also, in the future, avoid replying to the mailinglist digest, or at
the very least please trim off the irrelevant quoted posts in your
reply.
> #print ("Length \t" + "Count\n")#print header for all numbers.
> for word, count in freq.items():
> print(len(word), count)
>
> fileProcess()
Cheers,
Chris
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: print header for output Chris Rebert <clp2@rebertia.com> - 2011-06-18 23:43 -0700 Re: print header for output Ben Finney <ben+python@benfinney.id.au> - 2011-06-19 19:34 +1000
csiph-web