我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.char()。
def capitalize(a): """ Return a copy of `a` with only the first character of each element capitalized. Calls `str.capitalize` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like of str or unicode Input array of strings to capitalize. Returns ------- out : ndarray Output array of str or unicode, depending on input types See also -------- str.capitalize Examples -------- >>> c = np.array(['a1b2','1b2a','b2a1','2a1b'],'S4'); c array(['a1b2', '1b2a', 'b2a1', '2a1b'], dtype='|S4') >>> np.char.capitalize(c) array(['A1b2', '1b2a', 'B2a1', '2a1b'], dtype='|S4') """ a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'capitalize')
def lower(a): """ Return an array with the elements converted to lowercase. Call `str.lower` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like, {str, unicode} Input array. Returns ------- out : ndarray, {str, unicode} Output array of str or unicode, depending on input type See also -------- str.lower Examples -------- >>> c = np.array(['A1B C', '1BCA', 'BCA1']); c array(['A1B C', '1BCA', 'BCA1'], dtype='|S5') >>> np.char.lower(c) array(['a1b c', '1bca', 'bca1'], dtype='|S5') """ a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'lower')
def swapcase(a): """ Return element-wise a copy of the string with uppercase characters converted to lowercase and vice versa. Calls `str.swapcase` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like, {str, unicode} Input array. Returns ------- out : ndarray, {str, unicode} Output array of str or unicode, depending on input type See also -------- str.swapcase Examples -------- >>> c=np.array(['a1B c','1b Ca','b Ca1','cA1b'],'S5'); c array(['a1B c', '1b Ca', 'b Ca1', 'cA1b'], dtype='|S5') >>> np.char.swapcase(c) array(['A1b C', '1B cA', 'B cA1', 'Ca1B'], dtype='|S5') """ a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'swapcase')
def title(a): """ Return element-wise title cased version of string or unicode. Title case words start with uppercase characters, all remaining cased characters are lowercase. Calls `str.title` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like, {str, unicode} Input array. Returns ------- out : ndarray Output array of str or unicode, depending on input type See also -------- str.title Examples -------- >>> c=np.array(['a1b c','1b ca','b ca1','ca1b'],'S5'); c array(['a1b c', '1b ca', 'b ca1', 'ca1b'], dtype='|S5') >>> np.char.title(c) array(['A1B C', '1B Ca', 'B Ca1', 'Ca1B'], dtype='|S5') """ a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'title')
def upper(a): """ Return an array with the elements converted to uppercase. Calls `str.upper` element-wise. For 8-bit strings, this method is locale-dependent. Parameters ---------- a : array_like, {str, unicode} Input array. Returns ------- out : ndarray, {str, unicode} Output array of str or unicode, depending on input type See also -------- str.upper Examples -------- >>> c = np.array(['a1b c', '1bca', 'bca1']); c array(['a1b c', '1bca', 'bca1'], dtype='|S5') >>> np.char.upper(c) array(['A1B C', '1BCA', 'BCA1'], dtype='|S5') """ a_arr = numpy.asarray(a) return _vec_string(a_arr, a_arr.dtype, 'upper')
def capitalize(self): """ Return a copy of `self` with only the first character of each element capitalized. See also -------- char.capitalize """ return asarray(capitalize(self))
def count(self, sub, start=0, end=None): """ Returns an array with the number of non-overlapping occurrences of substring `sub` in the range [`start`, `end`]. See also -------- char.count """ return count(self, sub, start, end)
def decode(self, encoding=None, errors=None): """ Calls `str.decode` element-wise. See also -------- char.decode """ return decode(self, encoding, errors)
def encode(self, encoding=None, errors=None): """ Calls `str.encode` element-wise. See also -------- char.encode """ return encode(self, encoding, errors)
def endswith(self, suffix, start=0, end=None): """ Returns a boolean array which is `True` where the string element in `self` ends with `suffix`, otherwise `False`. See also -------- char.endswith """ return endswith(self, suffix, start, end)
def find(self, sub, start=0, end=None): """ For each element, return the lowest index in the string where substring `sub` is found. See also -------- char.find """ return find(self, sub, start, end)
def index(self, sub, start=0, end=None): """ Like `find`, but raises `ValueError` when the substring is not found. See also -------- char.index """ return index(self, sub, start, end)
def isalnum(self): """ Returns true for each element if all characters in the string are alphanumeric and there is at least one character, false otherwise. See also -------- char.isalnum """ return isalnum(self)
def isalpha(self): """ Returns true for each element if all characters in the string are alphabetic and there is at least one character, false otherwise. See also -------- char.isalpha """ return isalpha(self)
def isdigit(self): """ Returns true for each element if all characters in the string are digits and there is at least one character, false otherwise. See also -------- char.isdigit """ return isdigit(self)
def isspace(self): """ Returns true for each element if there are only whitespace characters in the string and there is at least one character, false otherwise. See also -------- char.isspace """ return isspace(self)
def istitle(self): """ Returns true for each element if the element is a titlecased string and there is at least one character, false otherwise. See also -------- char.istitle """ return istitle(self)
def isupper(self): """ Returns true for each element if all cased characters in the string are uppercase and there is at least one character, false otherwise. See also -------- char.isupper """ return isupper(self)
def join(self, seq): """ Return a string which is the concatenation of the strings in the sequence `seq`. See also -------- char.join """ return join(self, seq)
def ljust(self, width, fillchar=' '): """ Return an array with the elements of `self` left-justified in a string of length `width`. See also -------- char.ljust """ return asarray(ljust(self, width, fillchar))
def lstrip(self, chars=None): """ For each element in `self`, return a copy with the leading characters removed. See also -------- char.lstrip """ return asarray(lstrip(self, chars))
def replace(self, old, new, count=None): """ For each element in `self`, return a copy of the string with all occurrences of substring `old` replaced by `new`. See also -------- char.replace """ return asarray(replace(self, old, new, count))
def rfind(self, sub, start=0, end=None): """ For each element in `self`, return the highest index in the string where substring `sub` is found, such that `sub` is contained within [`start`, `end`]. See also -------- char.rfind """ return rfind(self, sub, start, end)
def rindex(self, sub, start=0, end=None): """ Like `rfind`, but raises `ValueError` when the substring `sub` is not found. See also -------- char.rindex """ return rindex(self, sub, start, end)
def rjust(self, width, fillchar=' '): """ Return an array with the elements of `self` right-justified in a string of length `width`. See also -------- char.rjust """ return asarray(rjust(self, width, fillchar))
def rstrip(self, chars=None): """ For each element in `self`, return a copy with the trailing characters removed. See also -------- char.rstrip """ return asarray(rstrip(self, chars))
def split(self, sep=None, maxsplit=None): """ For each element in `self`, return a list of the words in the string, using `sep` as the delimiter string. See also -------- char.split """ return split(self, sep, maxsplit)
def splitlines(self, keepends=None): """ For each element in `self`, return a list of the lines in the element, breaking at line boundaries. See also -------- char.splitlines """ return splitlines(self, keepends)
def startswith(self, prefix, start=0, end=None): """ Returns a boolean array which is `True` where the string element in `self` starts with `prefix`, otherwise `False`. See also -------- char.startswith """ return startswith(self, prefix, start, end)
def strip(self, chars=None): """ For each element in `self`, return a copy with the leading and trailing characters removed. See also -------- char.strip """ return asarray(strip(self, chars))
def title(self): """ For each element in `self`, return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase. See also -------- char.title """ return asarray(title(self))
def translate(self, table, deletechars=None): """ For each element in `self`, return a copy of the string where all characters occurring in the optional argument `deletechars` are removed, and the remaining characters have been mapped through the given translation table. See also -------- char.translate """ return asarray(translate(self, table, deletechars))
def upper(self): """ Return an array with the elements of `self` converted to uppercase. See also -------- char.upper """ return asarray(upper(self))
def zfill(self, width): """ Return the numeric string left-filled with zeros in a string of length `width`. See also -------- char.zfill """ return asarray(zfill(self, width))
def isnumeric(self): """ For each element in `self`, return True if there are only numeric characters in the element. See also -------- char.isnumeric """ return isnumeric(self)
def write_nifti(data, output_fname, header=None, affine=None, use_data_dtype=True, **kwargs): """Write data to a nifti file. This will write the output directory if it does not exist yet. Args: data (ndarray): the data to write to that nifti file output_fname (str): the name of the resulting nifti file, this function will append .nii.gz if no suitable extension is given. header (nibabel header): the nibabel header to use as header for the nifti file. If None we will use a default header. affine (ndarray): the affine transformation matrix use_data_dtype (boolean): if we want to use the dtype from the data instead of that from the header when saving the nifti. **kwargs: other arguments to Nifti2Image from NiBabel """ if header is None: header = nib.nifti2.Nifti2Header() if use_data_dtype: header = copy.deepcopy(header) dtype = data.dtype if data.dtype == np.bool: dtype = np.char try: header.set_data_dtype(dtype) except nib.spatialimages.HeaderDataError: pass if not (output_fname.endswith('.nii.gz') or output_fname.endswith('.nii')): output_fname += '.nii.gz' if not os.path.exists(os.path.dirname(output_fname)): os.makedirs(os.path.dirname(output_fname)) if isinstance(header, nib.nifti2.Nifti2Header): format = nib.Nifti2Image else: format = nib.Nifti1Image format(data, affine, header=header, **kwargs).to_filename(output_fname)