0

I wrote a program that can find the way through a labyrinth. This program works with a matrix.

Now I want to know how I can convert this string:

XXXXXXXXXXXXXXXIXXX
X                 X
X XXXXXXXXXXXXXXXXX
X             X   X
X XXX XXXXX X X X X
X   X X   X X X X X
X X X XXX X X X X X
X X X     X X   X X
XXXXXXXXXXXOXXXXXXX

into a matrix like this:

data = [["X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "I", "X", "X", "X"], 
        ["X", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "X"], 
        ["X", " ", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X"], 
        ["X", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", "X", " ", " ", " ", "X"], 
        ["X", " ", "X", "X", "X", " ", "X", "X", "X", "X", "X", " ", "X", " ", "X", " ", "X", " ", "X"], 
        ["X", " ", " ", " ", "X", " ", "X", " ", " ", " ", "X", " ", "X", " ", "X", " ", "X", " ", "X"], 
        ["X", " ", "X", " ", "X", " ", "X", "X", "X", " ", "X", " ", "X", " ", "X", " ", "X", " ", "X"], 
        ["X", " ", "X", " ", "X", " ", " ", " ", " ", " ", "X", " ", "X", " ", " ", " ", "X", " ", "X"], 
        ["X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "X", "O", "X", "X", "X", "X", "X", "X", "X"]] 
5
  • 3
    What have you already tried (Post some code! Explain it!)? What happened (wrong result, error etc.)? Commented Mar 18, 2015 at 13:50
  • 1
    Is it necessary to convert types from str to list? Both are iterables and interchangeable in may cases. Commented Mar 18, 2015 at 13:50
  • My programm works. Its just this string that i dont know how to convert into a matrix. And i realy have no idea how could i do that for a string that has no fixed size Commented Mar 18, 2015 at 13:52
  • 1
    @Sven: Your labyrinth program uses a matrix that's a list of lists of strings, with each string being a single cell of the matrix. But myaut is saying that you can instead simply use a list of strings, with each string being a row of the matrix. And that will require only very minor changes to your code, and possibly no changes at all, depending on how you've written your code. Commented Mar 18, 2015 at 14:01
  • 1
    It's great that your labyrinth program works, but pureferret is asking you to post some code that attempts to convert the string into a matrix, since that's the topic of your question. Questions without relevant code risk getting closed. See stackoverflow.com/help/mcve Commented Mar 18, 2015 at 14:06

2 Answers 2

1

Worked for me:

[[x for x in line] for line in a.split('\n')]

Example:

>>> a = """
... XXXXXXXXXXXXXXXIXXX
... X                 X
... X XXXXXXXXXXXXXXXXX
... X             X   X
... X XXX XXXXX X X X X
... X   X X   X X X X X
... X X X XXX X X X X X
... X X X     X X   X X
... XXXXXXXXXXXOXXXXXXX
... """


>>> [[x for x in line] for line in a.split('\n')]
[[], ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'I', 'X', 'X', 'X'], ['X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'], ['X', ' ', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], ['X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X', ' ', ' ', ' ', 'X'], ['X', ' ', 'X', 'X', 'X', ' ', 'X', 'X', 'X', 'X', 'X', ' ', 'X', ' ', 'X', ' ', 'X', ' ', 'X'], ['X', ' ', ' ', ' ', 'X', ' ', 'X', ' ', ' ', ' ', 'X', ' ', 'X', ' ', 'X', ' ', 'X', ' ', 'X'], ['X', ' ', 'X', ' ', 'X', ' ', 'X', 'X', 'X', ' ', 'X', ' ', 'X', ' ', 'X', ' ', 'X', ' ', 'X'], ['X', ' ', 'X', ' ', 'X', ' ', ' ', ' ', ' ', ' ', 'X', ' ', 'X', ' ', ' ', ' ', 'X', ' ', 'X'], ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'O', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], []]

And to remove empty sublists:

[z for z in ([x for x in line] for line in a.split('\n')) if z]
Sign up to request clarification or add additional context in comments.

1 Comment

As jonrsharpe said in his comment on blue_note's answer, list(line) is better than [x for x in line].
1

assuming you string is defined as

text = '''
X....
'''

you get the matrix by

matrix = [[str(char) for char in line] for line in text.splitlines()]

2 Comments

Why not just [list(line) for line in ...]? It's already a string, there's really no point explicitly "converting" each char to str.
@jonrsharpe: you' re right, I was just in "list comprehension" thinking mode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.