Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7946 > unrolled thread
| Started by | Chris Rebert <clp2@rebertia.com> |
|---|---|
| First post | 2011-06-18 23:43 -0700 |
| Last post | 2011-06-19 19:34 +1000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-06-18 23:43 -0700 |
| Subject | Re: print header for output |
| Message-ID | <mailman.141.1308465791.1164.python-list@python.org> |
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
[toc] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-06-19 19:34 +1000 |
| Message-ID | <87zkleqi4s.fsf@benfinney.id.au> |
| In reply to | #7946 |
Chris Rebert <clp2@rebertia.com> writes: > 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. Right. It will help communication greatly if you reply inline with specific quoted material (what Wikipedia calls “interleaved style”) <URL:https://secure.wikimedia.org/wikipedia/en/wiki/Posting_style#Interleaved_style> in the form of a conversation. Remove the quoted material you're not responding to; if you're not responding to anything in a message, don't reply to that message and instead compose a new message. Hope that helps! -- \ “If you always want the latest and greatest, then you have to | `\ buy a new iPod at least once a year.” —Steve Jobs, MSNBC | _o__) interview 2006-05-25 | Ben Finney
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web