2

So I was trying to do a maze and wanted to transform the maze into a two-dimensional array so it would be easier to manipulate, but I didn't really succeed. The best I've done is a list of string with the following :

map = """
OOOOOOOOOO
O O    O O
O . OO   O
O O O   XO
O OOOO O.O
O O O    U
O OOOOOO.O
O O      O
O O OOOOOO
O . O    O
OOOOOOOOOO
"""
map = [i for i in map.splitlines()]

Thanks for the help

2
  • 6
    Try [list(i) for i in map.splitlines()]. Commented Oct 21, 2019 at 13:34
  • Just an FYI, with your map string as it's currently written, you'll have an empty list at position 0 (since multi-line strings preserve all newlines). If you don't want this, you can add a \` to skip the first newline -> map = """\` Commented Oct 21, 2019 at 13:56

1 Answer 1

1

splitlines() returns a list of strings. Each string being a line.

So when you call it on your example, it returns a list of strings. If you want a list of lists, you must convert each string to a list. This can be done easily with the list() function :

map = [list(line) for line in map.splitlines()]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.