如何在python中将多个列表合并为一个列表? [重复]

问题描述 投票:0回答:1

可能重复:Making a flat list out of list of lists in PythonJoin a list of lists together into one list in Python

我有很多列表,看起来像

['it']
['was']
['annoying']

我希望上面看起来像

['it', 'was', 'annoying']

我该如何实现?

python nltk
1个回答
151
投票

只需添加它们:


139
投票
import itertools
ab = itertools.chain(['it'], ['was'], ['annoying'])
list(ab)

47
投票
a = ['it']
b = ['was']
c = ['annoying']

a.extend(b)
a.extend(c)

# a now equals ['it', 'was', 'annoying']
© www.soinside.com 2019 - 2024. All rights reserved.