python怎么将多个列表合并

lewis 2017-07-08 18次阅读

在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'],即将两个列表合并成一个新的列表。



发表评论:

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