双循环,多个" if"条件语句

时间:2015-11-03 16:06:40

标签: python for-loop numpy conditional

我有两个数组" a"对应于"坐标"在我的问题中,b对应于坐标的特定值。 我试图了解" a"我得到了" b"中的所有3个值。例如,我想打印[2,4,6]的行,因为我在" b" 但没有出现......有一个错误...

import numpy as np

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

b = np.array([2,4,6,8,10])

for i in range(0,a.shape[0]):
    for j in range(0,b.shape[0]):
        if (b[j]==a[i,0] and b[j]==a[i,1]):
            print i

2 个答案:

答案 0 :(得分:2)

试试这个:

@Override
    public void onDraw(Canvas canvas, Rect bounds) {
        // Get the current Time
        mTime.setToNow();

        // Set the current accelerometer readings.
        accelerometerX.setText("X : " + String.valueOf(sensorX));
        accelerometerY.setText("Y : " + String.valueOf(sensorY));
        accelerometerZ.setText("Z : " + String.valueOf(sensorZ));
        accelerometerRateOfChange.setText("T: " + String.valueOf(totalDisplacement));
    }

答案 1 :(得分:1)

由于您已经在使用numpy,因此此处也可以使用

import numpy as np

a[np.all(np.in1d(a,b).reshape(a.shape),axis=1)]