Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这样的设计,例如:
design = """xxx yxx xyx"""
我想将其转换为数组、矩阵、嵌套列表,如下所示:
[['x', 'x', 'x'], ['y', 'x', 'x'], ['x', 'y', 'x']]
请问你会怎么做?
str.splitlines与map或一起使用list comprehension:
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']]