python bug in this list implementation?

Chris Smith NOSPAM at NO.SPAM.com
Wed Dec 28 08:29:41 CET 2005


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]



begin 666 sudoku.txt
M,"PP+# L,2PU+# L,"PW+# -"C$L,"PV+# L,"PP+#@L,BPP#0HS+# L,"PX
M+#8L,"PP+#0L, T*#0HY+# L,"PT+# L,"PU+#8L-PT*,"PP+#0L-RPP+#@L
M,RPP+# -"C<L,RPR+# L,2PV+# L,"PT#0H-"C L-"PP+# L."PQ+# L,"PY
F#0HP+#$L-RPP+# L,"PR+# L. T*,"PU+# L,"PS+#<L,"PP+# `
`
end




More information about the Python-list mailing list