python 列表合并

  列表合并主要有以下方法:

  1、用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部

    

    结果:[1, 2, 3, 4, 5, 1, 20, 30]

  2、用切片(slice)操作,L1[len(L1):len(L1)] = L2和上面的方法等价

    

    结果:[1, 2, 3, 4, 5, 1, 20, 30]

    切片方法用起来更灵活,可以插入到头部,或其他任意部位,例如:

    加到开头:

    

    结果:[10, 20, 30, 1, 2, 3, 4, 5]

    加到中间:

    

    结果:[1, 10, 20, 30, 2, 3, 4, 5]

 

原文地址:https://www.cnblogs.com/shaosks/p/7145957.html