NumPy startswith()函数

原文:https://www.studytonight.com/numpy/numpy-startswith-function

在本教程中,我们将介绍 Numpy 库中 char 模块的startswith()功能。

startswith()函数返回一个布尔数组,其值可以是。如果给定字符串以函数中指定的前缀值开始,该函数将返回。因此这个函数名副其实。

如果与数组一起使用,这个函数调用str.startswith来执行数组的所有字符串元素。

startswith()的语法:

使用该函数所需的语法如下:

numpy.char.startswith(a, prefix, start=0, end=None)

以上语法表明isspace()函数取 4 个参数。

在上面的语法中,参数“a”主要用于指示将应用此函数的字符串的输入数组。

参数:

现在让我们讨论这个函数的参数:

  • a 该参数表示字符串或 Unicode 的数组。

  • 前缀 该参数表示在所有字符串中检查的前缀值。

  • 开始,结束 这两个参数都是可选的。使用可选的开始参数,搜索从该位置开始,使用可选的结束参数,功能在该位置停止比较。

返回值:

这个函数返回一个布尔值数组。

例 1:

如果前缀确实存在于输入字符串中,让我们检查这个函数的输出:

import numpy as np

arr = "The Grass is greener on the other side always"
print("The Input:\n",arr)
prefix = 'Th'

x = np.char.startswith(arr, prefix, start = 0, end = None)
print("The output is :")
print (x)

输入: 那边的草总是更绿 输出为: 真

例 2:

如果输入字符串中不存在前缀,让我们检查这个函数的输出:

import numpy as np

arr = "The Grass is greener on the other side always"
print("The Input:\n",arr)
prefix = 'It'

x = np.char.startswith(arr, prefix, start = 0, end = None)
print("The output is :")
print (x)

输入: 那边的草总是更绿 输出为: 假

摘要

在本教程中,我们学习了 Numpy 库的startswith()功能。我们介绍了如何使用它的语法和这个函数返回的值。有几个例子是为了了解这个函数的工作原理。