How to add an extra column to a NumPy array

前端 未结 17 2081
一个人的身影
一个人的身影 2020-11-22 14:37

Let’s say I have a NumPy array, a:

a = np.array([
    [1, 2, 3],
    [2, 3, 4]
    ])

And I would like to add a column of ze

17条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 15:14

    A bit late to the party, but nobody posted this answer yet, so for the sake of completeness: you can do this with list comprehensions, on a plain Python array:

    source = a.tolist()
    result = [row + [0] for row in source]
    b = np.array(result)
    

提交回复
热议问题