Python列表理解加入列表列表

时间:2013-02-11 07:30:32

标签: python list-comprehension

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

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

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

2 个答案:

答案 0 :(得分:77)

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))

答案 1 :(得分:4)

from itertools import chain

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