geekdoc-python-zh/docs/pythoncentral/how-to-merge-lists-in-pytho...

21 lines
912 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 如何在 Python 中合并列表
> 原文:<https://www.pythoncentral.io/how-to-merge-lists-in-python/>
如果您曾经想要将 Python 中的多个列表合并到一个列表中不要惊慌。Python 实际上使得执行这样的功能变得非常容易。您可以通过使用加号(+)来合并和连接列表——说真的,没有比这更简单的了。如果你想更好地了解合并列表是如何工作的,看看下面的例子。
假设我们有两个列表,它们是这样定义的:
```py
listone = [9, 13, 16]
listtwo = [21, 36, 54]
```
现在让我们假设我们想合并列表来创建一个列表。这是如何做到的:
```py
newlist = listone + listtwo
print newlist
```
上面代码的输出将是:91316213654。当然新列表的定义是这样的:newlist = [91316213654]。合并两个列表来创建一个新列表就是这么简单。