python合并两个数组的方法

2017-07-08  本文已影响95人  Hing0000

方法一:+

a=[1,2,3]
b=[4,5,6]
a=a+b

打印结果: [1, 2, 3, 4, 5, 6]

方法二:extend

c=[1,2,3]
d=[4,5,6]
c.extend(d)

打印结果:[1, 2, 3, 4, 5, 6]

使用append的时候要注意了。

方法三:append

d=[1,2,3]
e=[4,5,6]
d.append(e)

打印结果:[1, 2, 3, [4, 5, 6]]

f = [1,2,3]
g = [4,5,6]
mergedlist =[]
for elem in f:
  mergedlist.append(elem)
for elem in g:
  mergedlist.append(elem)
print mergedlist

打印结果:[1, 2, 3, 4, 5, 6]

上一篇 下一篇

猜你喜欢

热点阅读