我有这样的设计,例如:
design = """xxx
yxx
xyx"""
我想将它转换为数组、矩阵、嵌套列表,如下所示:
[['x', 'x', 'x'], ['y', 'x', 'x'], ['x', 'y', 'x']]
请问你会怎么做?
请您参考如下方法:
将 str.splitlines 与 map 或 list comprehension 一起使用:
使用 map :
>>> map(list, design.splitlines())
[['x', 'x', 'x'], ['y', 'x', 'x'], ['x', 'y', 'x']]
列表理解:
>>> [list(x) for x in design.splitlines()]
[['x', 'x', 'x'], ['y', 'x', 'x'], ['x', 'y', 'x']]
