Python列表推导加入列表列表

标签 python list-comprehension

给定 lists = [['hello'], ['world', 'foo', 'bar']]

如何将其转换为单个字符串列表?

combinedLists = ['hello', 'world', 'foo', 'bar']

最佳答案

lists = [['hello'], ['world', 'foo', 'bar']]
combined = [item for sublist in lists for item in sublist]

或者:

import itertools

lists = [['hello'], ['world', 'foo', 'bar']]
combined = list(itertools.chain.from_iterable(lists))

关于Python列表推导加入列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14807689/

相关文章:

haskell - `[ (x !! 0, x !! 1) | x <- mapM (const [' A', 'B' , 'C' ] ) [1..2], head x < head (tail x) ]` 是如何工作的?

Python time.sleep 关闭终端

php - 帮助我理解 PHP 变量引用和作用域

Haskell 列表理解映射

python - 枚举所有可能的二人组星座

python - 避免 FOR 循环将多个字符串 append 到列表

Python 列表理解语法错误

Python Pandas - 如何使用键的存在过滤具有包含字典的列的数据框?

python - 将 x 轴设置为一组值的时间(年、月)

python - 我想开发一个使用 gcc 编译和运行 c 程序的 python 脚本