鉴于lists = [['hello'], ['world', 'foo', 'bar']]
Python列表理解加入列表清单
如何将其转换为单个字符串列表?
combinedLists = ['hello', 'world', 'foo', 'bar']
鉴于lists = [['hello'], ['world', 'foo', 'bar']]
Python列表理解加入列表清单
如何将其转换为单个字符串列表?
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))
虽然第一选择看起来更好,在我看来。使用itertools要快得多。这个答案很好。 – 2013-02-11 07:40:42
from itertools import chain
combined = [['hello'], ['world', 'foo', 'bar']]
single = [i for i in chain.from_iterable(combined)]
我知道我可以通过使用嵌套循环做了很长的路要走,但我想知道,如果有一个一行做一样。 – congusbongus 2013-02-11 07:33:41