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: 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: References: 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 To: Cathy James 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Sat, Jun 18, 2011 at 9:57 PM, Cathy James wrote: > I managed to get output for my function, thanks much =C2=A0for 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 =C2=A0Length > 4 7 > 8 1 > 12 2 > > > Code so far: > def fileProcess(filename =3D open('declaration.txt', 'r')): > > =C2=A0 =C2=A0"""Call the program with an argument, > =C2=A0 =C2=A0it should treat the argument as a filename, > =C2=A0 =C2=A0splitting it up into words, and computes the length of each = word. > =C2=A0 =C2=A0print a table showing the word count for each of the word le= ngths > that has been encountered.""" > > =C2=A0 =C2=A0freq =3D {} #empty dict to accumulate word count and word le= ngth > =C2=A0 =C2=A0print ("Length \t" + "Count\n") > =C2=A0 =C2=A0for line in filename: > =C2=A0 =C2=A0 =C2=A0 =C2=A0punc =3D string.punctuation + string.whitespac= e#use Python's > built-in punctuation and whiitespace > =C2=A0 =C2=A0 =C2=A0 =C2=A0for word in (line.replace (punc, "").lower().s= plit()): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if word in freq: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0freq[word] +=3D1 #= increment current count if word already in dict > > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0else: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0freq[word] =3D 1 #= if punctuation encountered, > frequency=3D0 word length =3D 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. > =C2=A0 =C2=A0 =C2=A0 =C2=A0#print ("Length \t" + "Count\n")#print header = for all numbers. > =C2=A0 =C2=A0 =C2=A0 =C2=A0for word, count in freq.items(): > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0print(len(word), count) > > fileProcess() Cheers, Chris