这个问题在这里已经有了答案:





How to make a flat list out of a list of lists

(54 个回答)


8年前关闭。




给定 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/

10-13 07:13