Return (a * i), that is string multiple concatenation, element-wise. Values in `i` of less than 0 are treated as 0 (which yields an empty string). Parameters ---------- a : array_like of str or unicode i : array_like of ints Returns ------- out : ndar
(a, i)
| 342 | |
| 343 | @array_function_dispatch(_multiply_dispatcher) |
| 344 | def multiply(a, i): |
| 345 | """ |
| 346 | Return (a * i), that is string multiple concatenation, |
| 347 | element-wise. |
| 348 | |
| 349 | Values in `i` of less than 0 are treated as 0 (which yields an |
| 350 | empty string). |
| 351 | |
| 352 | Parameters |
| 353 | ---------- |
| 354 | a : array_like of str or unicode |
| 355 | |
| 356 | i : array_like of ints |
| 357 | |
| 358 | Returns |
| 359 | ------- |
| 360 | out : ndarray |
| 361 | Output array of str or unicode, depending on input types |
| 362 | |
| 363 | Examples |
| 364 | -------- |
| 365 | >>> a = np.array(["a", "b", "c"]) |
| 366 | >>> np.char.multiply(x, 3) |
| 367 | array(['aaa', 'bbb', 'ccc'], dtype='<U3') |
| 368 | >>> i = np.array([1, 2, 3]) |
| 369 | >>> np.char.multiply(a, i) |
| 370 | array(['a', 'bb', 'ccc'], dtype='<U3') |
| 371 | >>> np.char.multiply(np.array(['a']), i) |
| 372 | array(['a', 'aa', 'aaa'], dtype='<U3') |
| 373 | >>> a = np.array(['a', 'b', 'c', 'd', 'e', 'f']).reshape((2, 3)) |
| 374 | >>> np.char.multiply(a, 3) |
| 375 | array([['aaa', 'bbb', 'ccc'], |
| 376 | ['ddd', 'eee', 'fff']], dtype='<U3') |
| 377 | >>> np.char.multiply(a, i) |
| 378 | array([['a', 'bb', 'ccc'], |
| 379 | ['d', 'ee', 'fff']], dtype='<U3') |
| 380 | """ |
| 381 | a_arr = numpy.asarray(a) |
| 382 | i_arr = numpy.asarray(i) |
| 383 | if not issubclass(i_arr.dtype.type, integer): |
| 384 | raise ValueError("Can only multiply by integers") |
| 385 | out_size = _get_num_chars(a_arr) * max(int(i_arr.max()), 0) |
| 386 | return _vec_string( |
| 387 | a_arr, type(a_arr.dtype)(out_size), '__mul__', (i_arr,)) |
| 388 | |
| 389 | |
| 390 | def _mod_dispatcher(a, values): |