[Tutor] list problem

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 1 Jan 2001 19:19:54 -0800 (PST)


On Sun, 31 Dec 2000, W.W.van den Broek wrote:

> Trying to make a little "program" for myself, i am working my way
> through:  "Teach yourself python..". Found out that mathematical
> subject are the topics to begin with in simple programming. I am a
> psychiatrist so i am used to associative thinking and pattern
> recognition instead of logical thinking so bare with me please. As a
> psychiatrist i am doing some research. I wanted to


You don't need to go straight into mathy territory --- at least, not
traditional mathy territory.  You can try string or list-related stuff
like this:

Let's say we have a name:

    name = ["john", "doe"]

One common thing we can do is get the initials of a name.  We could do
this manually:

    initials = name[0][0], name[1][0]

But doing this manually might be a little tedious.  For example, if we had
a list of names, like:

    names = [ ["john", "doe"], ["jane", "doe"] ]

then trying to get the initials of each names becomes twice as tedious.  
However, things don't need to be so bad: let's try writing a function that
takes in a name, and returns its initials.

###
def getInitials(name):
    first_name, last_name = name
    return first_name[0], last_name[0]
###

Then we can use this to get the initials of each name in names:

###
    print getInitials(names[0])
    print getInitials(names[1])
###

or, more generally:

###
    for name in names:
        print getInitials(name)
###

Things like this might appeal more to you than taking the average of a
list of numbers.



> program some simple statistics, like finding the median of a list or a
> mean, range and quartiles.  Lists are probably useful in this, so
> started making a list in a file:  list.txt: l= [n, n1, n2,.....]

This sounds reasonable.  However, it might be better for list.txt to
contain only numbers.  For example, you can enter into your list.txt the
following:

###
3
1
4
1
5
9
2
6
###

The reason for this is because data files should be treated as pure data.  
By default, they won't be interpreted as Python statements (unless we do
something special like an exec_file function).  Here's a interpreter
session that shows how to read such a file:

###
>>> file = open("example.txt")
>>> file
<open file 'example.txt', mode 'r' at 80c8d18>
>>> numbers = file.readlines()                                                   
>>> numbers
['3\012', '1\012', '4\012', '1\012', '5\012', '9\012', '2\012', '6\012']
>>> numbers = map(int, numbers)
>>> numbers
[3, 1, 4, 1, 5, 9, 2, 6]
###

I'm using the readlines() method of a file to get all the input, since it
makes it easier to read in a list of lines.  Using read() will work, but
it returns back the results as one single string --- it requires an extra
step to break it down back to a list.

However, the tricky part is to realize that Python really doesn't know
that we have numbers --- as far as it can tell, they are strings.  So to
do any number stuff with them (like numerical sorting or comparisons), we
need to change all those strings into integers.

That's what the "map(int, numbers)" comes in --- semantically, it means:
"apply this int() function on each element in numbers".  The int()
function takes anything and tries to coerse it into an integer.  
Afterwards, you should be able to do stuff like:

    numbers.sort()

successfully.  What you had before:

###
x = open("list.txt", "rb")                                                      
x.read ()                                                                       
x.sort () 
###

doesn't quite work because the result of x.read() just "falls off" --- you
hadn't stored the result of x.read(), so it just disappears.


To make things easier for you, here's a function that takes in a file
name, slurps each line of that file, and returns a list containing the
respective numbers:

###
def getNumbers(filename):
    file = open(filename)
    return map(int, file.readlines())
###

Use getNumbers() above to read in your file data, and that should simplify
your file reading.

If you have any questions on this, please feel free to ask.