python列表理解到连接列表

Python list comprehension to join list of lists

本问题已经有最佳答案,请猛点这里访问。

lists = [['hello'], ['world', 'foo', 'bar']]为例

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

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


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

或: </P >

1
2
3
4
import itertools

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


1
2
3
4
from itertools import chain

combined = [['hello'], ['world', 'foo', 'bar']]
single = [i for i in chain.from_iterable(combined)]