Python列表理解以加入列表列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14807689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:29:44 来源:igfitidea点击:
Python list comprehension to join list of lists
提问by congusbongus
Given lists = [['hello'], ['world', 'foo', 'bar']]
给定的 lists = [['hello'], ['world', 'foo', 'bar']]
How do I transform that into a single list of strings?
如何将其转换为单个字符串列表?
combinedLists = ['hello', 'world', 'foo', 'bar']
combinedLists = ['hello', 'world', 'foo', 'bar']
采纳答案by Nicolas
lists = [['hello'], ['world', 'foo', 'bar']]
combined = [item for sublist in lists for item in sublist]
Or:
或者:
import itertools
lists = [['hello'], ['world', 'foo', 'bar']]
combined = list(itertools.chain.from_iterable(lists))
回答by akira
from itertools import chain
combined = [['hello'], ['world', 'foo', 'bar']]
single = [i for i in chain.from_iterable(combined)]

