2

I have the current string:

output = "S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S"    

I want to convert it to the following list:

coordinates = [['S', '.', '#', '#', '.', '.', '.', '.', '.', '.'],
               ['#', '.', '#', '#', '.', '.', '#', '#', '#', '.'],
               ['#', '.', '#', '#', '#', '.', '#', '#', '#', '.'],
               ['#', '.', '.', '.', '.', '.', '#', '#', '#', '.'],
               ['#', '#', '#', '.', '#', '#', '#', '#', '#', 'S']]    

Basically every time I get a "\n" character, I want the sequence before it to be split like above and put in a separate list item. I tried the following:

    coordinates = []
    temp = []
    for item in output:
        if item != "\n":
            temp += item
        else:
            coordinates += temp

but it doesn't work, neither it works with

.append()

4 Answers 4

4

Split on newlines with str.splitlines() (to avoid producing an empty last line), then call list() on each resulting string. You can do this all in one expression with a list comprehension:

[list(line) for line in output.splitlines()]

Calling list() on a string creates a list object of the individual characters:

>>> list('foo')
['f', 'o', 'o']

Demo:

>>> from pprint import pprint
>>> output = "S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S"    
>>> [list(line) for line in output.splitlines()]
[['S', '.', '#', '#', '.', '.', '.', '.', '.', '.'], ['#', '.', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', '.', '#', '#', '#', '.'], ['#', '.', '.', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', '.', '#', '#', '#', '#', '#', 'S']]
>>> pprint(_)
[['S', '.', '#', '#', '.', '.', '.', '.', '.', '.'],
 ['#', '.', '#', '#', '.', '.', '#', '#', '#', '.'],
 ['#', '.', '#', '#', '#', '.', '#', '#', '#', '.'],
 ['#', '.', '.', '.', '.', '.', '#', '#', '#', '.'],
 ['#', '#', '#', '.', '#', '#', '#', '#', '#', 'S']]

Your attempt fails because you never reset temp; you'd need to create a new empty list each time you reach \n, and use coordinates.append() instead of += (which is the same as coordinates.extend():

coordinates = []
temp = []
for item in output:
    if item != "\n":
        temp += item
    else:
        coordinates.append(temp)
        temp = []
if temp:
    coordinates.append(temp)

You need the last two lines to not miss the last line after the last '\n' character.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Martijn: can you please explain how _ is set?
@linuxfan: the Python terminal sets _ to the last echoed result. So any expression you execute that doesn't return None has the result printed to the terminal, and _ is set.
I prefer this answer over .split('\n') since I'm presuming it's line-ending style agnostic (Windows vs. Unix).
@DavidSanders: it is.
@DavidSanders: I used it here because 'foo\nbar\n'.split('\n') produces a list with three elements, but 'foo\nbar\n'.splitlines() produces a list with only two elements.
|
2

You could use a list comprehension to generate each list of characters between the \n delimeter:

>>> [list(x) for x in output.split('\n')]
[['S', '.', '#', '#', '.', '.', '.', '.', '.', '.'],
 ['#', '.', '#', '#', '.', '.', '#', '#', '#', '.'],
 ['#', '.', '#', '#', '#', '.', '#', '#', '#', '.'],
 ['#', '.', '.', '.', '.', '.', '#', '#', '#', '.'],
 ['#', '#', '#', '.', '#', '#', '#', '#', '#', 'S']]

Passing a string x to list returns a list of the characters in x. output.split('\n') is one way to break your string output into a list of strings which were separated by the newline character \n.

Edit: The answer written by @MartijnPieters highlighted an important point about splitting stings on \n (which I admit I wasn't aware of).

If your string has a trailing \n it may be preferable to use output.splitlines() to split the string. Here, using split('\n') would return an empty string after the final \n, whereas splitlines() avoids this by default.

Comments

1

You can split on newlines '\n' then convert to lists.

output = "S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S"
coordinates = [list(s) for s in output.split('\n')]

>>> coordinates
[['S', '.', '#', '#', '.', '.', '.', '.', '.', '.'],
 ['#', '.', '#', '#', '.', '.', '#', '#', '#', '.'],
 ['#', '.', '#', '#', '#', '.', '#', '#', '#', '.'],
 ['#', '.', '.', '.', '.', '.', '#', '#', '#', '.'],
 ['#', '#', '#', '.', '#', '#', '#', '#', '#', 'S']]

Comments

1

This should work:

output = "S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S"
items = output.split('\n')
final_list = []
for item in items:
    line_list = [c for c in item]
    final_list.append(line_list)


print final_list

Comments

Your Answer

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