[Tutor] List indexing problem
Mike Meisner
mikem at blazenetme.net
Sat Jul 26 00:05:07 CEST 2008
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080725/86d7d736/attachment.htm>
More information about the Tutor
mailing list