61

与えられたlists = [['hello'], ['world', 'foo', 'bar']]

それを文字列の単一のリストに変換するにはどうすればよいですか?

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

4

2 に答える 2

118
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))
于 2013-02-11T07:33:52.127 に答える
8
from itertools import chain

combined = [['hello'], ['world', 'foo', 'bar']]
single = [i for i in chain.from_iterable(combined)]
于 2013-02-11T07:33:02.663 に答える