numpy.char.asarray#
- char.asarray(obj,itemsize=None,unicode=None,order=None)[source]#
Convert the input to a
chararray, copying the data only ifnecessary.Versus a NumPy array of dtype
bytes_orstr_, thisclass adds the following functionality:values automatically have whitespace removed from the endwhen indexed
comparison operators automatically remove whitespace from theend when comparing values
vectorized string operations are provided as methods(e.g.
chararray.endswith)and infix operators (e.g.+,*,%)
- Parameters:
- objarray of str or unicode-like
- itemsizeint, optional
itemsize is the number of characters per scalar in theresulting array. Ifitemsize is None, andobj is anobject array or a Python list, theitemsize will beautomatically determined. Ifitemsize is provided andobjis of type str or unicode, then theobj string will bechunked intoitemsize pieces.
- unicodebool, optional
When true, the resulting
chararraycan contain Unicodecharacters, when false only 8-bit characters. If unicode isNone andobj is one of the following:then the unicode setting of the output array will beautomatically determined.
- order{‘C’, ‘F’}, optional
Specify the order of the array. If order is ‘C’ (default), then thearray will be in C-contiguous order (last-index varies thefastest). If order is ‘F’, then the returned arraywill be in Fortran-contiguous order (first-index varies thefastest).
Examples
>>>importnumpyasnp>>>np.char.asarray(['hello','world'])chararray(['hello', 'world'], dtype='<U5')