May-17-2019, 06:50 AM
(This post was last modified: May-17-2019, 06:50 AM by Gribouillis.)
Here is how you could print every pair of identical rows
from collections import defaultdict import itertools as itt import numpy as np a=np.matrix([[1,2,3],[42,68,69],[1,2,3],[85,89,95]]) b=np.matrix([[42,68,69],[1,2,3],[85,89,95], [42,68,69]]) def group_lines(m): d = defaultdict(list) for i, row in enumerate(m.A): d[tuple(row)].append(i) return d def identical_rows(ma, mb): da, db = (group_lines(m) for m in (ma, mb)) for row, na in da.items(): if row in db: yield from itt.product(na, db[row]) if __name__ == '__main__': for i, j in identical_rows(a, b): print(i, j, a.A[i])The result is
Output:3 2 [85 89 95]
1 0 [42 68 69]
1 3 [42 68 69]
0 1 [1 2 3]
2 1 [1 2 3]