python - 如何在python中按索引创建列表中的项目列表
问题描述
我有一个清单m
:
m = ['ABC', 'XYZ', 'LMN']
我想要输出如下:
m = [['a','x','l']
['b','y','m']
['c','z','n']]
如何才能做到这一点?
解决方案
用于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']]
推荐阅读
- unity3d - MeshRenderer 旋转时边界错误
- python-3.x - 如何创建一个新的哈希表
- java - 覆盖 png 文件
- python - 如何从两个不同的python模块调用变量(双向)
- python - 为什么excel文件上传为zip文件?
- php - 在联系表 7 中集成 SOAP
- ios - iOS 13 窗口背景颜色
- ruby - 当使用 `execute` 而不是 `invoke` 运行任务时,为什么 `args` 不是 Rake::TaskArguments 的实例?
- flutter - Flutter in_app_purchase 0.2.1 purchaseUpdatedStream.listen 在 Android 上不起作用
- angular-material - 当侧面导航在角度材料中打开时如何添加侧面导航切换图标