发布时间:2021-10-14 03:49:48 人气:549 作者:admin
Python列表类型list合并有哪些方法?
在Python编程语言中,有各种数据类型,比如字符串、列表、集合、有序集合、数组等等,对于刚接触Python的同学来说,可能会有些陌生和畏惧,本篇文章将为大家带来Python中list合并的4种方式,如果你还不太清楚,请看下文:
方法一、直接使用"+"号合并列表
aList = [3,4,5];
bList = ['abc','Python'];
cList = aList + bList;
dList = bList + aList;print(cList);print(dList);
//输出为//[3,4,5,'abc','Python'];//['abc','Python',3,4,5]
方法二、使用extend方法
aList = [1,3,4];
bList = ['abc','Python_study'];
aList.extend(bList);print(aList);
//输出为//[1,3,4,'abc','Python_study'];
方法三、使用切片
aList = [1,2,3];
bList = ['aaa','Python_class'];
aList[len(aList):len(aList)] = bList;print(aList);
//输出为//[1,2,3,'aaa','Python_class'];
方法四、使用append方法
aList = [1,2,3];
bList = ['aaa','Python_class'];
aList.append(bList);print(aList);
//输出为//[1,2,3,['aaa','Python_class']];
以上是关于Python列表类型list合并的几个方法,由多测师亲自撰写。https://www.e70w.com/