[Tutor] Letter Frequency Count.

Doug Stanfield DOUGS@oceanic.com
Sat, 8 Jan 2000 11:03:58 -1000


You're right.  I always have used readline() or readlines() thus my
confusion.  To clarify definitively, here are abridged definitions from the
"Library Reference" (underline emphasis mine):

read([size]) 
Read at most size bytes from the file (less if the read hits EOF before
obtaining size bytes). If the size argument is negative or omitted, read all
data until EOF is reached. The bytes are returned as _a_string_object._ 

readline([size]) 
Read one entire line from the file. A trailing newline character is kept in
the _string_ (but may be absent when a file ends with an incomplete line).

readlines([sizehint]) 
Read until EOF using readline() and return a _list_ containing the lines
thus read. 

In the example case using read() allows the following with no nesting
necessary to pass over the lines;

for individual in emperor:
    if individual in simpsons:
        if countDict.has_key(individual):
            countDict[individual] = countDict[individual] + 1
        else:
            countDict[individual] = 1

Thanks,
-Doug-

> -----Original Message-----
> From: Evgeny Roubinchtein [mailto:eroubinc@u.washington.edu]
> Sent: Saturday, January 08, 2000 10:06 AM
> To: Doug Stanfield
> Cc: tutor@python.org
> Subject: RE: [Tutor] Letter Frequency Count.
> 
> 
> On Sat, 8 Jan 2000, Doug Stanfield wrote:
> 
> [...]
> >Where individual should be each single character from the 
> file.  In your
> >case this won't work straight off because you've read the 
> whole file in. You
> >might need to do the following to get to each character 
> assuming you just
> >want to throw away newlines:
> >
> >for line in emperor:
> >      for individual in line:
> >            # ... process individual ...
> >
> [...]
> 
> Well,  am confused by this:  I thought the file object's read() method
> returns a single string, not a list of strings -- unlike 
> readlines() -- so
> what's the need for nested loops?
> 
> --
> Evgeny 
> 
> BOMB: Burn Out Memory Banks
>