numpy的 - 添加行阵 [英] Numpy - add row to array

查看:244
本文介绍了numpy的 - 添加行阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个人如何行添加到一个数组numpy的?

How does one add rows to a numpy array?

我有一个数组答:

A = array([[0, 1, 2], [0, 2, 0]])

谨行从另一个阵列X添加到这个数组,如果在X各自行的第一个元素满足特定的条件。

I wish to add rows to this array from another array X if the first element of each row in X meets a specific condition.

numpy的数组没有一种方法'追加'这样的列表,或者看起来是这样。

Numpy arrays do not have a method 'append' like that of lists, or so it seems.

如果A和X分别列出我只会做的:

If A and X were lists I would merely do:

for i in X:
    if i[0] < 3:
        A.append(i)

有没有的 numpythonic 的办法做到相当于?

谢谢,
S; - )

Thanks, S ;-)

推荐答案

什么是 X ?如果它是一个二维数组,你怎么能那么它的行比作一个数字: I&LT; 3

What is X? If it is a 2D-array, how can you then compare its row to a number: i < 3?

OP的评论后编辑:

A = array([[0, 1, 2], [0, 2, 0]])
X = array([[0, 1, 2], [1, 2, 0], [2, 1, 2], [3, 2, 0]])

X 添加到 A 所有行的第一个元素&LT; 3

add to A all rows from X where the first element < 3:

A = vstack((A, X[X[:,0] < 3]))

# returns: 
array([[0, 1, 2],
       [0, 2, 0],
       [0, 1, 2],
       [1, 2, 0],
       [2, 1, 2]])

这篇关于numpy的 - 添加行阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆