This seems like a very simple process, but I cannot get the data out of the loop:


I have a data file "slink.txt":

88
10112213
33332332
22011220
23110122
21231102
12222321
32131132
10023232

I am trying to store the numbers in a 2 dimensional array:

f = open('slink.txt', 'r')

rows = int(f.read(1))
cols = int(f.read(1))
f.read(1) #skip endline
temp = []
data = []

for row in range(rows):
    for col in range(cols):
        temp.append(int(f.read(1)))
    f.read(1)
    data.append(temp)
    print '\n\nRow:', row, temp, '\nData: ', data #debug
    del temp[:]
   

print '\n\n\n',data

A sample of my output shows:

Row: 0 [1, 0, 1, 1, 2, 2, 1, 3] 
Data:  [[1, 0, 1, 1, 2, 2, 1, 3]]


Row: 1 [3, 3, 3, 3, 2, 3, 3, 2] 
Data:  [[3, 3, 3, 3, 2, 3, 3, 2], [3, 3, 3, 3, 2, 3, 3, 2]]


Row: 2 [2, 2, 0, 1, 1, 2, 2, 0] 
Data:  [[2, 2, 0, 1, 1, 2, 2, 0], [2, 2, 0, 1, 1, 2, 2, 0], [2, 2, 0, 1, 1, 2, 2, 0]]

.
.
.

[[], [], [], [], [], [], [], []]

The last line shows the data variable to be empty. Something doesn't seem right. Please help.

Ben

Recommended Answers

All 5 Replies

It's got to be the way your data file has been written. Try this ...

str1 = """\
88
10112213
33332332
22011220
23110122
21231102
12222321
32131132
10023232
"""
# save your data file
fout = open('slink.txt', 'w')
fout.write(str1)
fout.close()


f = open('slink.txt', 'r')

rows = int(f.read(1))
cols = int(f.read(1))
f.read(1) #skip endline
temp = []
data = []

for row in range(rows):
    for col in range(cols):
        temp.append(int(f.read(1)))
    f.read(1)
    data.append(temp)
    print '\nRow:', row, temp, '\nData: ', data   #debug
    temp = []  # or del temp[:]

print '\n\n\n',data

Not sure it has anything to do with the data file. I tried the same algorithm using a 2d dummy variable, instead of reading data in from a file, with only the same results. Maybe something with the append module?

Its the del temp [:] that is messing with the append function.

When I substitute your temp = [] everything works well...


Thank you

I believe this is due to the way Python handles variables.

When you append an element, you are effectively appending a reference, in this case a reference to an array. Each time you run the loop, you empty the array.

When you write del temp [:] you are telling Python to delete the contents of the variable pointed to by temp.

When you write temp = [], you are telling Python to make temp now point to a new empty array.

Hmm, that wasn't a very good explanation. Here are a couple of examples. First of all:

>>> a = [1]
>>> b = []
>>> b.append(a)
>>> b
[[1]]
>>> a.append(2)
>>> b
[[1, 2]]

Here we say a points to an array consisting of just 1, and append it to b, which points to an empty list. When we inspect the contents of b, it's what we expect.

Then, if we append 2 to a, we actually append 2 to the array pointed to by a - this is the same array that is pointed to in the first element of b, so the first element of b changes as well.

If instead we use this code:

>>> a = [1]
>>> b = []
>>> b.append(a)
>>> b
[[1]]
>>> a = [1,2]
>>> b
[[1]]

It behaves differently. Rather than changing the array that is pointed to by a, a = [1,2] says that we create a brand new array, containing 1 and 2, and make a point to this new array. Therefore, the array that a used to point to is unchanged, and the first element of the b is unchanged.

Similarly, when you write del temp[:] you affect an element of data, since this changes the object that both temp and an element of data point to. When you write temp = [], you are reassigning temp so that it points to something else, which therefore does not affect any elements of data.

Hopefully that makes some sort of sense!

Thank you, I appreciate the explination. I kind of figured it was a pointer issue when I learned not to use the del command. I'll tell you, it was confusing the heck out of me before I posted...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.