Find indexes of matching rows in two 2-D arrays

前端 未结 3 1665
广开言路
广开言路 2021-02-13 18:36

Suppose that I have two 2-D arrays as follows:

array([[3, 3, 1, 0],
       [2, 3, 1, 3],
       [0, 2, 3, 1],
       [1, 0, 2, 3],
       [3, 1, 0, 2]], dtype=in         


        
3条回答
  •  臣服心动
    2021-02-13 18:54

    I can't think of a numpy specific way to do it, but here's what I would do with regular lists:

    >>> L1= [[3, 3, 1, 0],
    ...        [2, 3, 1, 3],
    ...        [0, 2, 3, 1],
    ...        [1, 0, 2, 3],
    ...        [3, 1, 0, 2]]
    >>> L2 = [[0, 3, 3, 1],
    ...        [0, 2, 3, 1],
    ...        [1, 0, 2, 3],
    ...        [3, 1, 0, 2],
    ...        [3, 3, 1, 0]]
    >>> L1 = {tuple(row):i for i,row in enumerate(L1)}
    >>> answer = []
    >>> for i,row in enumerate(L2):
    ...   if tuple(row) in L1:
    ...     answer.append((L1[tuple(row)], i))
    ... 
    >>> answer
    [(2, 1), (3, 2), (4, 3), (0, 4)]
    

提交回复
热议问题