python将两个数组合并成一个数组的两种方法

2022-04-15 00:00:00 合并 数组 两种

python中可以通过数组的extend方法对原数组进行扩展的方法来实现数组的合并,也可以通过加号操作符进行合并

"""
皮蛋编程(https://www.pidancode.com)
创建日期:2022/4/3
功能描述:python将两个数组合并成一个数组的两种方法
"""
# 方法1
c1 = ["Red","Green","Blue"]
c2 = ["Orange","Yellow","Indigo"]
c1.extend(c2)
assert c1 == ["Red","Green","Blue","Orange","Yellow","Indigo"]

#方法2
c1 = ["Red","Green","Blue"]
c2 = ["Orange","Yellow","Indigo"]
c3 = c1 + c2
assert c3 == ["Red","Green","Blue","Orange","Yellow","Indigo"]

相关文章