zoukankan      html  css  js  c++  java
  • list的append与extend

    1.针对一维

    append: 列表添加一个元素(直接嵌套一个list)

    l1 = [2, 3, 4]
    l2 = [7, 8, 9]
    l1.append(l2)
    l1
    
    [2, 3, 4, [7, 8, 9]]

    extend:两个列表合并:

    l1 = [2, 3, 4]
    l2 = [7, 8, 9]
    l1.extend(l2)
    l1
    
    [2, 3, 4, 7, 8, 9]

    2.针对二维的情况

    appen:

    l1 = [[2, 3, 4],[2, 3, 4]]
    l2 = [[7, 8, 9],[7, 8, 9]]
    l1.append(l2)
    
    >>> l1
    [[2, 3, 4], [2, 3, 4], [[7, 8, 9], [7, 8, 9]]]

    extend:

    l1 = [[2, 3, 4],[2, 3, 4]]
    l2 = [[7, 8, 9],[7, 8, 9]]
    l1.extend(l2)
    
    >>> l1
    [[2, 3, 4], [2, 3, 4], [7, 8, 9], [7, 8, 9]]

    3.二维形成一个新的list 

    extend:

    import numpy as np
    l=[]
    l1 = [[2, 3, 4],[2, 3, 4]]
    l2 = [[7, 8, 9],[7, 8, 9]]
    l.extend(l1)
    l.extend(l2)
    
    >>> l
    [[2, 3, 4], [2, 3, 4], [7, 8, 9], [7, 8, 9]]
    >>> np.concatenate(l,axis=0)
    array([2, 3, 4, 2, 3, 4, 7, 8, 9, 7, 8, 9])

    append:

    import numpy as np
    l=[]
    l1 = [[2, 3, 4],[2, 3, 4]]
    l2 = [[7, 8, 9],[7, 8, 9]]
    l.append(l1)
    l.append(l2)
    
    >>> l
    [[[2, 3, 4], [2, 3, 4]], [[7, 8, 9], [7, 8, 9]]]
    >>> np.concatenate(l,axis=0)
    array([[2, 3, 4],
           [2, 3, 4],
           [7, 8, 9],
           [7, 8, 9]])
  • 相关阅读:
    01-HTML控件
    08-多线程
    07-Log日志
    06-File-文件
    05-函数式编程
    04-异常使用处理
    03-常用包模块
    02-包
    Java NIO(六) Selector
    Java NIO(四) Scatter/Gather
  • 原文地址:https://www.cnblogs.com/BlueBlueSea/p/15773541.html
Copyright © 2011-2022 走看看