Numpy 的 np.char.split(~)
方法將字符串數組作為輸入,並對每個字符串應用 Python 的 split(~)
方法。 split()
方法使用指定的分隔符(即分隔符)對字符串進行分區。
請注意,原始數組保持不變,並返回數組的全新副本。
參數
1. a
| array-like
要將其字符串轉換為大寫的輸入數組。
2. sep
| str
或 unicode
| optional
用於分割字符串的分隔符。默認情況下,sep=" "
(即單個空格)。
3. maxsplit
| int
| optional
要執行的最大分割數。分區總數為 maxsplit+1
。默認情況下,沒有設置最大限製。
返回值
包含分區字符串的 Numpy 列表數組。
例子
按空間分割
要按空格分割,請勿指定 sep
:
np.char.split(["Hello there", "I am a cat"])
array([list(['Hello', 'there']), list(['I', 'am', 'a', 'cat'])], dtype=object)
請注意,返回值是一個包含分區字符串的列表的 Numpy 數組。
指定分隔符
用逗號分隔:
np.char.split(["Hello,there", "I,am,a,cat"], sep = ',')
array([list(['Hello', 'there']), list(['I', 'am', 'a', 'cat'])], dtype=object)
指定最大分割
假設我們想要最多進行 2 次分割:
np.char.split(["I am a cat"], maxsplit=2)
array([list(['I', 'am', 'a cat'])], dtype=object)
如果沒有 maxsplit,我們將獲得 4 個分區(即 3 個分割)。
相關用法
- Python NumPy char find方法用法及代碼示例
- Python NumPy char less_equal方法用法及代碼示例
- Python NumPy char greater方法用法及代碼示例
- Python NumPy char equal方法用法及代碼示例
- Python NumPy char lower方法用法及代碼示例
- Python NumPy char less方法用法及代碼示例
- Python NumPy char greater_equal方法用法及代碼示例
- Python NumPy char multiply方法用法及代碼示例
- Python NumPy char upper方法用法及代碼示例
- Python NumPy char not_equal方法用法及代碼示例
- Python NumPy char isupper方法用法及代碼示例
- Python NumPy char add方法用法及代碼示例
- Python NumPy char count方法用法及代碼示例
- Python NumPy char rjust方法用法及代碼示例
- Python NumPy char rstrip方法用法及代碼示例
- Python NumPy char ljust方法用法及代碼示例
- Python Wand charcoal()用法及代碼示例
- Python numpy chararray.tostring用法及代碼示例
- Python numpy char.chararray.tostring用法及代碼示例
- Python NumPy choose方法用法及代碼示例
- Python chr方法用法及代碼示例
- Python chr()用法及代碼示例
- Python NumPy choice方法用法及代碼示例
- Python cudf.core.column.string.StringMethods.is_vowel用法及代碼示例
- Python cudf.Series.ceil用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy char | split method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。