Returns an array with the number of non-overlapping occurrences of substring `sub` in the range [`start`, `end`]. Calls `str.count` element-wise. Parameters ---------- a : array_like of str or unicode sub : str or unicode The substring to search for. start
(a, sub, start=0, end=None)
| 521 | |
| 522 | @array_function_dispatch(_count_dispatcher) |
| 523 | def count(a, sub, start=0, end=None): |
| 524 | """ |
| 525 | Returns an array with the number of non-overlapping occurrences of |
| 526 | substring `sub` in the range [`start`, `end`]. |
| 527 | |
| 528 | Calls `str.count` element-wise. |
| 529 | |
| 530 | Parameters |
| 531 | ---------- |
| 532 | a : array_like of str or unicode |
| 533 | |
| 534 | sub : str or unicode |
| 535 | The substring to search for. |
| 536 | |
| 537 | start, end : int, optional |
| 538 | Optional arguments `start` and `end` are interpreted as slice |
| 539 | notation to specify the range in which to count. |
| 540 | |
| 541 | Returns |
| 542 | ------- |
| 543 | out : ndarray |
| 544 | Output array of ints. |
| 545 | |
| 546 | See Also |
| 547 | -------- |
| 548 | str.count |
| 549 | |
| 550 | Examples |
| 551 | -------- |
| 552 | >>> c = np.array(['aAaAaA', ' aA ', 'abBABba']) |
| 553 | >>> c |
| 554 | array(['aAaAaA', ' aA ', 'abBABba'], dtype='<U7') |
| 555 | >>> np.char.count(c, 'A') |
| 556 | array([3, 1, 1]) |
| 557 | >>> np.char.count(c, 'aA') |
| 558 | array([3, 1, 0]) |
| 559 | >>> np.char.count(c, 'A', start=1, end=4) |
| 560 | array([2, 1, 1]) |
| 561 | >>> np.char.count(c, 'A', start=1, end=3) |
| 562 | array([1, 0, 0]) |
| 563 | |
| 564 | """ |
| 565 | return _vec_string(a, int_, 'count', [sub, start] + _clean_args(end)) |
| 566 | |
| 567 | |
| 568 | def _code_dispatcher(a, encoding=None, errors=None): |