| | |
No documentation on reading in int from file
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 8
Reputation:
Solved Threads: 1
This seems like a very simple process, but I cannot get the data out of the loop:
I have a data file "slink.txt":
I am trying to store the numbers in a 2 dimensional array:
A sample of my output shows:
The last line shows the data variable to be empty. Something doesn't seem right. Please help.
Ben
I have a data file "slink.txt":
Python Syntax (Toggle Plain Text)
88 10112213 33332332 22011220 23110122 21231102 12222321 32131132 10023232
I am trying to store the numbers in a 2 dimensional array:
Python Syntax (Toggle Plain Text)
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:
Python Syntax (Toggle Plain Text)
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
It's got to be the way your data file has been written. Try this ...
python Syntax (Toggle Plain Text)
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
Last edited by vegaseat; Apr 5th, 2008 at 7:30 pm.
May 'the Google' be with you!
•
•
Join Date: Mar 2008
Posts: 4
Reputation:
Solved Threads: 1
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:
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:
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!
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:
python Syntax (Toggle Plain Text)
>>> 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:
python Syntax (Toggle Plain Text)
>>> 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!
![]() |
Similar Threads
- Safe Array (C++)
- Tutorial: An introduction to Test Driven Development (Computer Science)
- SMBIOS, Reading Hardware Information (C)
- Using OpenGL in Visual C++: Part I (Game Development)
- libtiff questions (C++)
- Creating a "Data" folder in my C: Drive using my Java program (Java)
- Open In New Window Php (PHP)
- localtime_s, C++ 2005 Express (C++)
- helllp with loops (C++)
Other Threads in the Python Forum
- Previous Thread: text adventure game "local variable refferenced before assignment"
- Next Thread: saving a picture from a webpage every hour
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically event events examples external file float format frange function google gui hints homework i/o iframe import input jaunty java line linux list lists loop matching mouse multiple newb number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop word wxpython






