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

重负在身 分享于 2小时前 24405阅 0人收藏此代码, 我要收藏

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

c1 = ["Red","Green","Blue"]
c2 = ["Orange","Yellow","Indigo"]
c1.extend(c2)
 
assert c1 == ["Red","Green","Blue","Orange","Yellow","Indigo"]

下面使用+号操作符

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

标签:
  • 数组合并
  • extend
  • python