numpy.char.multiply#

char.multiply(a, i)[source]#

返回 (a * i),即字符串重复连接,元素级操作。

i 中小于 0 的值被视为 0(这将产生一个空字符串)。

参数:
aarray_like,dtype 为 np.bytes_np.str_
iarray_like,具有任意整数 dtype
返回:
outndarray

输出数组为 str 或 unicode 类型,具体取决于输入类型

说明

这是对 np.strings.multiply 的轻量级封装,当 i 不是整数时会引发 ValueError。它的存在仅是为了向后兼容。

示例

>>> import numpy as np
>>> a = np.array(["a", "b", "c"])
>>> np.strings.multiply(a, 3)
array(['aaa', 'bbb', 'ccc'], dtype='<U3')
>>> i = np.array([1, 2, 3])
>>> np.strings.multiply(a, i)
array(['a', 'bb', 'ccc'], dtype='<U3')
>>> np.strings.multiply(np.array(['a']), i)
array(['a', 'aa', 'aaa'], dtype='<U3')
>>> a = np.array(['a', 'b', 'c', 'd', 'e', 'f']).reshape((2, 3))
>>> np.strings.multiply(a, 3)
array([['aaa', 'bbb', 'ccc'],
       ['ddd', 'eee', 'fff']], dtype='<U3')
>>> np.strings.multiply(a, i)
array([['a', 'bb', 'ccc'],
       ['d', 'ee', 'fff']], dtype='<U3')