python中两个列表合并:
a=[1,2,3,4,5,6]
b=['a','b','c','d']
方法1:
a+b如下:
>>>a=[1,2,3,4,5,6]
>>>b=['a','b','c','d']
>>>a+b
[1,2,3,4,5,6,'a','b','c','d']
方法2:
a+=b这时a的值变成了合并后的结果,如下:
>>>a=[1,2,3,4,5,6]
>>>b=['a','b','c','d']
>>>a+=b
>>>a
[1,2,3,4,5,6,'a','b','c','d']
方法3:
a.extend(b)和+=结果一样,输出a如下:
>>>a=[1,2,3,4,5,6]
>>>b=['a','b','c','d']
>>>a.extend(b)
>>>a
[1,2,3,4,5,6,'a','b','c','d']
合并方法4:
a.append(b)将b看成list一个元素和a合并成一个新的list,合并后的结果输入a如下:
>>>a=[1,2,3,4,5,6]
>>>b=['a','b','c','d']
>>>a.append(b)
>>>a
[1,2,3,4,5,6,['a','b','c','d']]
以上内容为大家介绍了python怎样将两个list合并,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:瀚银百科。