因为两个数组中的任何一个都可以被洗牌以匹配另一个,所以没有人阻止我们重新排列这两个数组。使用Jaime's Answer,我们可以vstack两个数组并找到唯一的行。unique返回的反向索引本质上就是所需的映射(因为数组不包含重复的行)。在
为了方便起见,我们先定义一个unique2d函数:def unique2d(arr,consider_sort=False,return_index=False,return_inverse=False):
"""Get unique values along an axis for 2D arrays.
input:
arr:
2D array
consider_sort:
Does permutation of the values within the axis matter?
Two rows can contain the same values but with
different arrangements. If consider_sort
is True then those rows would be considered equal
return_index:
Similar to numpy unique
return_inverse:
Similar to numpy unique
returns:
2D array of unique rows
If return_index is True also returns indices
If return_inverse is True also returns the inverse array
"""