Numpy Split 函数

NumPyNumPyBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将介绍 Numpy 库中 char 模块的 split() 函数。split() 函数用于根据指定的分隔符将输入字符串拆分为字符串列表。

虚拟机提示

虚拟机启动完成后,点击左上角切换到 Notebook 选项卡以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,随时可以向 Labby 提问。实验结束后请提供反馈,我们将及时为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) numpy(("NumPy")) -.-> numpy/FileInputOutputGroup(["File Input/Output"]) numpy(("NumPy")) -.-> numpy/ArrayBasicsGroup(["Array Basics"]) numpy(("NumPy")) -.-> numpy/ArrayManipulationGroup(["Array Manipulation"]) python/BasicConceptsGroup -.-> python/strings("Strings") numpy/ArrayBasicsGroup -.-> numpy/data_array("Data to Array") numpy/ArrayManipulationGroup -.-> numpy/split("Split") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/ModulesandPackagesGroup -.-> python/using_packages("Using Packages") python/ModulesandPackagesGroup -.-> python/standard_libraries("Common Standard Libraries") numpy/FileInputOutputGroup -.-> numpy/text_io("Text File Input/Output") python/PythonStandardLibraryGroup -.-> python/data_collections("Data Collections") subgraph Lab Skills python/strings -.-> lab-86502{{"Numpy Split 函数"}} numpy/data_array -.-> lab-86502{{"Numpy Split 函数"}} numpy/split -.-> lab-86502{{"Numpy Split 函数"}} python/build_in_functions -.-> lab-86502{{"Numpy Split 函数"}} python/importing_modules -.-> lab-86502{{"Numpy Split 函数"}} python/using_packages -.-> lab-86502{{"Numpy Split 函数"}} python/standard_libraries -.-> lab-86502{{"Numpy Split 函数"}} numpy/text_io -.-> lab-86502{{"Numpy Split 函数"}} python/data_collections -.-> lab-86502{{"Numpy Split 函数"}} end

导入 Numpy 库

为了使用 numpy.char.split() 函数,我们需要导入 numpy 库。可以通过以下代码实现:

import numpy as np

使用 split() 函数拆分简单字符串

在这一步中,我们将使用 split() 函数拆分一个简单的字符串。我们将定义一个字符串 input_string,并将其作为参数传递给 split() 函数。该函数的输出将是通过按空格拆分输入字符串得到的字符串列表。

input_string = "Hello World!"
result = np.char.split(input_string)
print("Input String: {}\nOutput String: {}".format(input_string,result))

上述代码的输出将是:

Input String: Hello World!
Output String: ['Hello', 'World!']

使用分隔符拆分字符串

在这一步中,我们将使用分隔符拆分一个字符串。我们将定义一个包含多个 | 分隔符的字符串 input_string。使用 split() 函数,我们将以 | 作为分隔符拆分字符串,并输出结果列表。

input_string = "apple|banana|cherry"
result = np.char.split(input_string, sep='|')
print("Input String: {}\nOutput String: {}".format(input_string,result))

上述代码的输出将是:

Input String: apple|banana|cherry
Output String: ['apple', 'banana', 'cherry']

使用最大拆分次数拆分字符串

在这一步中,我们将以最大拆分次数拆分一个字符串。我们将定义一个字符串 input_string,并仅拆分一次。使用 split() 函数,我们将以 | 作为分隔符,并将最大拆分次数限制为 1。

input_string = "apple|banana|cherry"
result = np.char.split(input_string, sep='|', maxsplit=1)
print("Input String: {}\nOutput String: {}".format(input_string,result))

上述代码的输出将是:

Input String: apple|banana|cherry
Output String: ['apple', 'banana|cherry']

拆分字符串数组

在这一步中,我们将拆分一个字符串数组。我们将使用 numpy 定义一个字符串数组。使用 split() 函数,我们将以 - 作为分隔符,并将最大拆分次数限制为 1,来拆分字符串数组。

input_array = np.array(['apple-juice', 'banana-milkshake', 'cherry-smoothie'])
result = np.char.split(input_array, sep='-', maxsplit=1)
print("Input Array: \n{}\nOutput Array: {}".format(input_array,result))

上述代码的输出将是:

Input Array:
['apple-juice' 'banana-milkshake' 'cherry-smoothie']
Output Array:
[list(['apple', 'juice']) list(['banana', 'milkshake']) list(['cherry', 'smoothie'])]

总结

在本实验中,我们学习了 Numpy 库中的 split() 函数。我们了解了如何根据指定的分隔符将字符串拆分为字符串列表。我们还介绍了在拆分字符串时如何使用最大拆分限制。最后,我们学习了如何使用 split() 函数拆分字符串数组。