python - 加入python中的列表列表

标签 python

在 python 中将列表加入单个列表(或迭代器)的简短语法是什么?

例如,我有一个如下列表,我想遍历 a、b 和 c。

x = [["a","b"], ["c"]]

我能想到的最好的如下。

result = []
[ result.extend(el) for el in x] 

for el in result:
  print el

最佳答案

import itertools
a = [['a','b'], ['c']]
print(list(itertools.chain.from_iterable(a)))

关于python - 加入python中的列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/716477/

相关文章:

python - 如何旋转图像以获得非空像素?

python - 使用读取的新值更新列表值 - Python

python - 无法将 MSGraph 与 python MSAL 一起使用

python - 将 Pandas groupby.groups 结果转换为数据帧,使用索引元组值作为数据帧的行和列名称

python - twinx 杀死蜱标签颜色

python - 从 python 脚本中获取 python 脚本的输出

python - 安装包时 JetBrains DataSpell ModuleNotFoundError 错误

c# - c# SecurityException 中的沙盒化 ironpython

python - 使用 Python/Shapely 聚合地理点的最佳方式

python - 在 pip 中获取 zip 文件时出现 403 错误