首页 > 解决方案 > 如何在python中按索引创建列表中的项目列表

问题描述

我有一个清单m

m = ['ABC', 'XYZ', 'LMN']

我想要输出如下:

m = [['a','x','l']
     ['b','y','m']
     ['c','z','n']]

如何才能做到这一点?

标签: pythonlist

解决方案


用于list(zip(*..))转置嵌套列表,并使用列表推导创建嵌套列表:

print(list(zip(*[list(i.lower()) for i in m])))

输出:

[('a', 'x', 'l'), ('b', 'y', 'm'), ('c', 'z', 'n')]

如果希望子值是列表:

print(list(map(list,zip(*[list(i.lower()) for i in m]))))

输出:

[['a', 'x', 'l'], ['b', 'y', 'm'], ['c', 'z', 'n']]

推荐阅读