Linux 拨号vps windows公众号手机端

python怎么将多个列表合并

lewis 8年前 (2017-07-08) 阅读数 9 #程序编程
文章标签 python

在Python中,可以使用"+"运算符来合并多个列表。以下是几种常用的方法:

  1. 使用"+"运算符:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
merged_list = list1 + list2
  1. 使用extend()方法:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list1.extend(list2)
  1. 使用append()方法和循环:
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for item in list2:
    list1.append(item)

无论使用哪种方法,上述代码的输出结果都是 [1, 2, 3, 'a', 'b', 'c'],即将两个列表合并成一个新的列表。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门