查找两个numpy数组中匹配行的索引

时间:2017-09-14 01:47:45

标签: numpy

如何找到两个numpy数组之间完全匹配的行的索引。例如:

    char stat[13];

    cout<<"Enter Registration No";

    cin>>reg_no;

    fstream out("Intermediate.txt",ios::in|ios::out|ios::ate|ios::app);

    out.seekg(0,ios::beg);

    out.read((char*)&std,sizeof(std));

    while(!out.eof())

     {  

        record_no++;

        if(strcmp(std.get_reg_no(),reg_no)==0)

        {

            found=1;

            break;

        }

     }

    location=(record_no-1)*sizeof(std);

    out.seekp(location,ios::beg);

    char new_status[]="Addmitted"; // initialized to replace with status

    strcpy(std.status,new_status);

    out.write((char*)&std,sizeof(std));

    out.close();

这应该返回:

x = np.array(([0,1],
              [1,0],
              [0,0]))
y = np.array(([0,1],
              [1,1],
              [0,0]))

2 个答案:

答案 0 :(得分:3)

np.flatnonzero((x == y).all(1))
# array([0, 2])

或:

np.nonzero((x == y).all(1))[0]

或:

np.where((x == y).all(1))[0]

答案 1 :(得分:0)

如果长度相同,这适用于每对numpy数组:

matches = [i for i in range(len(x)) if x[i].tolist()==y[i].tolist()]