3D数组中的索引列表

时间:2019-11-24 00:36:40

标签: python numpy numpy-ndarray

我有一个看起来像这样的数组:

enteredPassword.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) { }

    @Override
    public void afterTextChanged(Editable enteredText) {
    enteredPassword.setText(enteredText.toString()); /* although this is actually weird! in the sense that enteredPassword already has the text and yet, you're still assigning it texts back to itself, I mean enteredPassword.setText(enteredPassword.getText().toString() is equivalent to enteredText.toString())*/
    }
});

我有3个索引的列表

 [[[ -1.,  1.,  -1.,  1.,  -1.,  1.,  1.,  1.,  1., -1.,  1.,  1.,  1.,  1.]],

 [[ 1.,  0.,  1.,  0.,  1.,  0.,  1.,  0.,  1., 0.,  1.,  0.,  1.,  0.]],

 [[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., 0.,  0.,  0.,  0.,  0.]]]

我只想获取那些索引为零的“行”。所以面具看起来像这样:

[2,3,4]

我要寻找的结果就是满足条件的两个“行”:

[False, True, True]

我将“行”用引号引起来,因为我知道其中还有一个额外的维度-但它需要保留。

======================扩展示例=================

 [[ 1.,  0.,  1.,  0.,  1.,  0.,  1.,  0.,  1., 0.,  1.,  0.,  1.,  0.]],

 [[ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0., 0.,  0.,  0.,  0.,  0.]]]

所以我正在寻找f()

1 个答案:

答案 0 :(得分:1)

解决方案1:

最Python化的方式(我的工作方式)。

c = [a[i] for i,j in enumerate(b) if a[i][0][j] == 1]

print(c)
[[[1, 1, 1]], [[1, 0, 1]], [[1, 0, 0]]]

解决方案2:

a = [[[0,1,0]], [[0,0,0]], [[1,1,1]], [[1,0,1]], [[0,0,1]], [[1,0,0]]]
b = [0, 1, 2, 2, 1, 0]

c=[]
for i,j in enumerate(b):
    if a[i][0][j] == 1:
        c.append(a[i])

print(c)
[[[1, 1, 1]], [[1, 0, 1]], [[1, 0, 0]]]