I need to do some statistical analysis by binning values into an array.

Being new to Python, I tried to use a list of lists.  I've extracted just the 
minimum code that I'm having trouble with:

def testanalysis():
    IP = []
    temp = [[0, 0, 0],[0, 0, 0],[0, 0, 0]]
    # initialize to zero
    for i in range(20):
        IP.append(temp)
    # increment each of the first five elements by 1
    for index in range(5):
        for type in range(3):
            for phase in range(3):
                IP[index][type][phase] += 1

    print IP 

The output is:

[[[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 
5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 
5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], 
[5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 
5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 
5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], 
[5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 
5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 
5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]], [[5, 5, 5], [5, 5, 5], [5, 5, 5]]]

What I wanted, of course, is to increment only the first 5 sub-lists to a value 
of 1 (not 5).

I'm obviously misunderstanding something fundamental about lists, list 
indexing, or both.

Is this enough information for someone to point out what I'm doing wrong?

Mike
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to