473,387 Members | 1,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

python bug in this list implementation?

Hi,

I've been working on some multi-dimensional lists and I've encountered some
very strange behaviour in what appears to be simple code, I'm using python
2.4.2 and IDLE. If anyone can tell me why it's behaving so strange please
let me know, any improvements to my general coding style are also
appreciated.
code below:

import sys
import copy

grid = []
oGrid = []
sGrid = []

def createGrid():
f = open(r"...sudoku.txt", "rb") ## see attached for the file.

for line in f:
aLine = line.strip().split(',')
if aLine != [""]:
for i in xrange(len(aLine)):
aLine[i] = int(aLine[i])
grid.append(aLine)

oGrid = copy.deepcopy(grid)
sGrid.append(copy.deepcopy(grid))
def printGrid():
print "original grid:"
for line in oGrid:
print line #why doesn't this print anything?

print "S grid:"
for line in sGrid:
print line #this prints the grid but the formatting is all over the
place.

print "Iteration grid: "
for line in grid:
print line #works fine!

if __name__ == "__main__":
createGrid()
printGrid()

##PRODUCES THE FOLLOWING OUTPUT:
##
##original grid:
##S grid:
##[[0, 0, 0, 1, 5, 0, 0, 7, 0], [1, 0, 6, 0, 0, 0, 8, 2, 0], [3, 0, 0, 8, 6,
0, 0, 4, 0], [9, 0, 0, 4, 0, 0, 5, 6, 7], [0, 0, 4, 7, 0, 8, 3, 0, 0], [7,
3, 2, 0, 1, 6, 0, 0, 4], [0, 4, 0, 0, 8, 1, 0, 0, 9], [0, 1, 7, 0, 0, 0, 2,
0, 8], [0, 5, 0, 0, 3, 7, 0, 0, 0]]
##Iteration grid:
##[0, 0, 0, 1, 5, 0, 0, 7, 0]
##[1, 0, 6, 0, 0, 0, 8, 2, 0]
##[3, 0, 0, 8, 6, 0, 0, 4, 0]
##[9, 0, 0, 4, 0, 0, 5, 6, 7]
##[0, 0, 4, 7, 0, 8, 3, 0, 0]
##[7, 3, 2, 0, 1, 6, 0, 0, 4]
##[0, 4, 0, 0, 8, 1, 0, 0, 9]
##[0, 1, 7, 0, 0, 0, 2, 0, 8]
##[0, 5, 0, 0, 3, 7, 0, 0, 0]

Dec 28 '05 #1
5 1115
Chris Smith wrote:
I've been working on some multi-dimensional lists and I've encountered some
very strange behaviour in what appears to be simple code, I'm using python
2.4.2 and IDLE. If anyone can tell me why it's behaving so strange please
let me know, any improvements to my general coding style are also
appreciated.
code below:

import sys
import copy

grid = []
oGrid = []
sGrid = []

def createGrid():
f = open(r"...sudoku.txt", "rb") ## see attached for the file.

for line in f:
aLine = line.strip().split(',')
if aLine != [""]:
for i in xrange(len(aLine)):
aLine[i] = int(aLine[i])
grid.append(aLine)
at this point, grid contains a list of lists.
oGrid = copy.deepcopy(grid)
if you assign to a name inside a function, that name is considered to be
*local*, unless you specify otherwise. in other words, this doesn't touch
the *global* (module-level) oGrid variable.
sGrid.append(copy.deepcopy(grid))
here you add a list of lists to a list. the result is a list with a single item.
def printGrid():
print "original grid:"
for line in oGrid:
print line #why doesn't this print anything?
because the *global* oGrid is still empty.
print "S grid:"
for line in sGrid:
print line #this prints the grid but the formatting is all over the
place.
because sGrid contains a single item; a copy of your original grid.
print "Iteration grid: "
for line in grid:
print line #works fine!


as expected.

I suggest reading up on list methods and global variables in your favourite
python tutorial.

also read:

http://www.catb.org/~esr/faqs/smart-...html#id3001405

</F>

Dec 28 '05 #2
On Wed, 28 Dec 2005 07:29:41 +0000, Chris Smith wrote:
Hi,

I've been working on some multi-dimensional lists and I've encountered some
very strange behaviour in what appears to be simple code, I'm using python
2.4.2 and IDLE. If anyone can tell me why it's behaving so strange please
let me know, any improvements to my general coding style are also
appreciated.
How about some advice about how to ask smart questions?

*What* is "so strange" about the behaviour -- what are you expecting? You
shouldn't expect people to read your mind and guess what behaviour you
expect, you should say "I expect X Y and Z, but I get A B and C instead."
In the future, you may not have somebody like Fredrik willing to read your
code trying to guess what you think it should do.

code below:

import sys
import copy

grid = []
oGrid = []
sGrid = []
Using global variables is not usually recommended. It is usually better to
pass local variables from function to function. That will avoid the bugs
which your code has, which Fredrik has already pointed out.

def createGrid():
f = open(r"...sudoku.txt", "rb") ## see attached for the file.
Why do you need a raw string? It isn't wrong to do one, but it is rather
unusual and unnecessary.
[snip] oGrid = copy.deepcopy(grid)
sGrid.append(copy.deepcopy(grid))


Why are you doing deepcopies of the lists? That seems to be unnecessary.

Oh, another bit of advice: any time you think you've found a bug in
Python, it almost always is a bug in your code.

--
Steven.

Dec 28 '05 #3
Steven D'Aprano wrote:
On Wed, 28 Dec 2005 07:29:41 +0000, Chris Smith wrote:

def createGrid():
f = open(r"...sudoku.txt", "rb") ## see attached for the file.


Why do you need a raw string? It isn't wrong to do one, but it is rather
unusual and unnecessary.


Chris is probably working on Windows where it is handy to enter paths as
raw strings because of the backslashes. Unusual however, and problematic
if you want to use the program on other platforms, is opening a text
file in binary mode.

-- Christoph
Dec 28 '05 #4
On Wed, 28 Dec 2005 15:07:52 +0100, Christoph Zwerschke wrote:
Steven D'Aprano wrote:
On Wed, 28 Dec 2005 07:29:41 +0000, Chris Smith wrote:
>>
def createGrid():
f = open(r"...sudoku.txt", "rb") ## see attached for the file.


Why do you need a raw string? It isn't wrong to do one, but it is rather
unusual and unnecessary.


Chris is probably working on Windows where it is handy to enter paths as
raw strings because of the backslashes. Unusual however, and problematic
if you want to use the program on other platforms, is opening a text
file in binary mode.


Sure, but then the file name doesn't have any backslashes.

I've never understood why binary mode is supposed to be bad. By my
understanding, binary mode simply means that no line endings are
translated, regardless of whether the file uses \n, \r or a combination of
the two. Surely the worst that can happen in binary mode is that your
strings end up having some carriage returns you have to deal with yourself?

--
Steven.

Dec 28 '05 #5
Steven D'Aprano wrote:
On Wed, 28 Dec 2005 15:07:52 +0100, Christoph Zwerschke wrote:
Chris is probably working on Windows where it is handy to enter paths as
raw strings because of the backslashes. Unusual however, and problematic
if you want to use the program on other platforms, is opening a text
file in binary mode.


I've never understood why binary mode is supposed to be bad. By my
understanding, binary mode simply means that no line endings are
translated, regardless of whether the file uses \n, \r or a combination of
the two. Surely the worst that can happen in binary mode is that your
strings end up having some carriage returns you have to deal with yourself?


Yes. The larger problem is when you don't explicitly use binary mode
while handling binary data on other platforms, and then port it to Windows.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Dec 28 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Mr.SpOOn | last post by:
Hi, I need a structure to represent a set of integers. I also need to perform on this set some basic set operations, such as adding or removing elements, joining with other sets and checking for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.