在Python編程語言中,有各種數(shù)據(jù)類型,比如字符串、列表、集合、有序集合、數(shù)組等等,對(duì)于剛接觸Python的同學(xué)來說,可能會(huì)有些陌生和畏懼,本篇文章將為大家?guī)鞵ython中l(wèi)ist合并的4種方式,如果你還不太清楚,請(qǐng)看下文:
方法一、直接使用"+"號(hào)合并列表
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']];