Test whether each element of an array is also present in a second array. The output is always a masked array. We recommend using :func:`isin` instead of `in1d` for new code. See Also -------- isin : Version of this function that preserves the shape of ar1.
(ar1, ar2, assume_unique=False, invert=False)
| 1385 | |
| 1386 | |
| 1387 | def in1d(ar1, ar2, assume_unique=False, invert=False): |
| 1388 | """ |
| 1389 | Test whether each element of an array is also present in a second |
| 1390 | array. |
| 1391 | |
| 1392 | The output is always a masked array. |
| 1393 | |
| 1394 | We recommend using :func:`isin` instead of `in1d` for new code. |
| 1395 | |
| 1396 | See Also |
| 1397 | -------- |
| 1398 | isin : Version of this function that preserves the shape of ar1. |
| 1399 | |
| 1400 | Examples |
| 1401 | -------- |
| 1402 | >>> import numpy as np |
| 1403 | >>> ar1 = np.ma.array([0, 1, 2, 5, 0]) |
| 1404 | >>> ar2 = [0, 2] |
| 1405 | >>> np.ma.in1d(ar1, ar2) |
| 1406 | masked_array(data=[ True, False, True, False, True], |
| 1407 | mask=False, |
| 1408 | fill_value=True) |
| 1409 | |
| 1410 | """ |
| 1411 | if not assume_unique: |
| 1412 | ar1, rev_idx = unique(ar1, return_inverse=True) |
| 1413 | ar2 = unique(ar2) |
| 1414 | |
| 1415 | ar = ma.concatenate((ar1, ar2)) |
| 1416 | # We need this to be a stable sort, so always use 'mergesort' |
| 1417 | # here. The values from the first array should always come before |
| 1418 | # the values from the second array. |
| 1419 | order = ar.argsort(kind='mergesort') |
| 1420 | sar = ar[order] |
| 1421 | if invert: |
| 1422 | bool_ar = (sar[1:] != sar[:-1]) |
| 1423 | else: |
| 1424 | bool_ar = (sar[1:] == sar[:-1]) |
| 1425 | flag = ma.concatenate((bool_ar, [invert])) |
| 1426 | indx = order.argsort(kind='mergesort')[:len(ar1)] |
| 1427 | |
| 1428 | if assume_unique: |
| 1429 | return flag[indx] |
| 1430 | else: |
| 1431 | return flag[indx][rev_idx] |
| 1432 | |
| 1433 | |
| 1434 | def isin(element, test_elements, assume_unique=False, invert=False): |
searching dependent graphs…