python怎么将多个列表合并

931
2023/12/5 6:19:14
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: python怎么安装tensorflow库