Pregunta

I have this design, for example:

design = """xxx
yxx
xyx"""

And I would like to convert it to an array, a matrix, nested lists, like this:

[['x', 'x', 'x'], ['y', 'x', 'x'], ['x', 'y', 'x']]

How would you do this, please?

¿Fue útil?

Solución

Use str.splitlines with either map or a list comprehension:

Using map:

>>> map(list, design.splitlines())
[['x', 'x', 'x'], ['y', 'x', 'x'], ['x', 'y', 'x']]

List Comprehension:

>>> [list(x) for x in  design.splitlines()]
[['x', 'x', 'x'], ['y', 'x', 'x'], ['x', 'y', 'x']]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top