Reading a text maze from a file, consists of a large series of walls (#) and openings ( ).
The maze needs to be read into a 2-dimensional array.
I can't figure out how to split each '#' and ' ' so that they are separate.
Reading a text maze from a file, consists of a large series of walls (#) and openings ( ).
The maze needs to be read into a 2-dimensional array.
I can't figure out how to split each '#' and ' ' so that they are separate.
Just calling list() should split each character individually as an element in a list
from pprint import pprint as pp
def loadMaze(mazeName):
global grandList
grandList = []
with open(mazeName) as sourceList:
for line in sourceList:
eachList = list(line)
grandList.append(eachList)
pp(grandList)
Don't call .split().
eachList = list(line.strip('\n'))
because string.split() is for separating a string by whitespace, into a list of sub-strings:
>>> "a bb ccc".split()
['a', 'bb', 'ccc']
or separating by a character:
>>> "a/bb/ccc".split('/')
['a', 'bb', 'ccc']
list(some_string) will make a list of the characters in the string:
>>> list("a, bb, ccc")
['a', ',', ' ', 'b', 'b', ',', ' ', 'c', 'c', 'c']
'#', - 5 characters each. A maze line is 180 characters in list form. PrettyPrint will try to keep lines to 80 characters, but that now doesn't fit, and is printing one item per line. You can make it do it with import pprint; pp = pprint.PrettyPrinter(width=200).pprint at the top instead, but the lines are wide. It's just not a great way to show the maze.print "" before the pp(grandList) line to just print an empty space and get away from the prompt.Yeap! You almost made it, just don't split the strings.
You can do it the easy way,
with open(mazeName) as sourceList:
for line in sourceList:
grandList.append(list(line))
pp(grandList)
or your own way:
with open(mazeName) as sourceList:
for line in sourceList:
grandList.append([c for c in line])
pp(grandList)