python - How does __contains__ work for ndarrays? -
>>> x = numpy.array([[1, 2], ... [3, 4], ... [5, 6]]) >>> [1, 7] in x true >>> [1, 2] in x true >>> [1, 6] in x true >>> [2, 6] in x true >>> [3, 6] in x true >>> [2, 3] in x false >>> [2, 1] in x false >>> [1, 2, 3] in x false >>> [1, 3, 5] in x false i have no idea how __contains__ works ndarrays. couldn't find relevant documentation when looked it. how work? , documented anywhere?
seems numpy's __contains__ doing 2-d case:
def __contains__(self, item): row in self: if any(item_value == row_value item_value, row_value in zip(item, row)): return true return false [1,7] works because 0th element of first row matches 0th element of [1,7]. same [1,2] etc. [2,6], 6 matches 6 in last row. [2,3], none of elements match row @ same index. [1, 2, 3] trivial since shapes don't match.
see this more, , this ticket.
Comments
Post a Comment