Python 程序 测试字符串是否包含列表中的元素

您所在的位置:网站首页 python3列表转字符串 Python 程序 测试字符串是否包含列表中的元素

Python 程序 测试字符串是否包含列表中的元素

2023-02-01 08:13| 来源: 网络整理| 查看: 265

Python 程序 测试字符串是否包含列表中的元素

在这篇文章中,我们将学习如何在Python中检查一个字符串是否包含一个列表中的元素。

使用的方法 使用嵌套的For循环

使用List Comprehension

使用any()函数

使用 find() 函数

例子

假设我们已经取得了一个 输入字符串 和 输入列表。 我们现在要检查输入字符串是否至少包含一个输入列表元素。

输入 inputString = "tutorialspoint is a best learning platform for coding" inputList = ['hello', 'tutorialspoint', 'python'] 输出 YES, the string contains elements from the input list

在上面的例子中,输入字符串包含 ” tutorialspoint” ,所以答案是肯定的。

方法1:使用嵌套的For循环 算法(步骤)

以下是执行所需任务时需要遵循的算法/步骤–。

创建一个变量来存储输入的字符串。

创建另一个变量来存储输入列表。

使用 split() 函数(将一个字符串分割成一个列表。我们可以定义分隔符;默认的分隔符是任何空格)将输入的字符串分割成一个单词的列表。

将一个临时的flag(temp_flag)变量初始化为0。

使用 for循环 遍历上述拆分后的单词列表。

使用另一个 嵌套的for循环 来遍历输入列表

使用 if条件 语句来检查两个元素是否相等。

如果条件为真,将 temp_flag 设为1。

如果temp_flag变为1,使用 break 语句中断循环。

使用if条件语句检查temp_flag的值是否为1。

打印结果

例子

下面的程序使用嵌套的for循环来检查字符串是否包含任何输入的列表元素:

# input string inputString = "tutorialspoint is a best learning platform for coding" # input list inputList = ['hello', 'tutorialspoint', 'python'] # printing the input string print("Input string:", inputString) # printing input list print("Input List:", inputList) # splitting the input string into a list of words wordsList = inputString.split(" ") # temporary flag variable temp_flag = 0 # traversing through the above-split words list for p in wordsList: # traversing through the input list for q in inputList: # checking whether both the elements are equal if p == q: # Set the value of temp_flag by 1 if the condition is true temp_flag = 1 # breaking from the loop if the temp_flag becomes 1 break # checking whether the value of temp_flag is 1 if temp_flag == 1: # printing "YES” if the condition is true print("YES, the string contains elements from the input list") else: # else print "NO" print("NO, the string does not contain elements from the input list") 输出

在执行时,上述程序将产生以下输出:

Input string: tutorialspoint is a best learning platform for coding Input List: ['hello', 'tutorialspoint', 'python'] YES, the string contains elements from the input list 方法2:使用List Comprehension 列表理解

当你希望根据现有列表的值建立一个新的列表时,列表理解提供了一个更短/更简洁的语法。

bool()函数 – 返回一个给定对象的布尔值

例子

下面的程序使用列表理解法检查输入的字符串是否包含任何输入的列表元素—-。

# input string inputString = "tutorialspoint is a best learning platform for coding" # input list inputList = ['hello', 'tutorialspoint', 'python'] # printing the input string print("Input string:", inputString) # printing input list print("Input List:", inputList) print() # checking whether the input string contains the list element # using list comprehension output = [i for i in inputList if(i in inputString)] # printing the resulting output as boolean print("Checking whether input string contains the list element:", bool(output)) 输出 Input string: tutorialspoint is a best learning platform for coding Input List: ['hello', 'tutorialspoint', 'python'] Checking whether input string contains the list element: True 方法3:使用any()函数

any()函数在迭代器中的任何项目为真时返回 真 ,否则返回假。

语法 any(iterable) 例子

下面的程序使用any()函数检查输入的字符串是否包含任何输入的列表元素。

# input string inputString = "tutorialspoint is a best learning platform for coding" # input list inputList = ['bat', 'cat', 'dog'] # printing the input string print("Input string:", inputString) # printing input list print("Input List:", inputList) print() # checking whether the input string contains the list element # using any() function output = any(k in inputString for k in inputList) print("Checking whether input string contains the list element:", bool(output)) 输出 Input string: tutorialspoint is a best learning platform for coding Input List: ['bat', 'cat', 'dog'] Checking whether input string contains the list element: False 方法4:使用find()函数

在这个方法中,我们使用 find() 方法查看该词是否存在于列表中,否则返回 -1 。

find()方法

查找给定值的第一次出现。如果没有找到该值,它返回-1。

语法 string.find(value, start, end) 例子

下面的程序使用find()函数检查输入的字符串是否包含任何输入的列表元素。

# input string inputString = "tutorialspoint is a best learning platform for coding" # input list inputList = ['bat', 'cat', 'dog'] # printing the input string print("Input string:", inputString) # printing input list print("Input List:", inputList) print() # Assuming the result as False initially reslt = False # intilializig a variable with 0 count = 0 # travsering through the input list for item in inputList: # checking whether the current list item is found in the string if(inputString.find(item) != -1): # incrementing the count value by 1 if the condition is true count += 1 # checking whether the count value is greater than or equal to 1 if(count >= 1): # assign result as True if the condition is true reslt = True print("Checking whether input string contains the list element:", bool(reslt)) 输出 Input string: tutorialspoint is a best learning platform for coding Input List: ['bat', 'cat', 'dog'] Checking whether input string contains the list element: False 总结

在这篇文章中,我们学习了如何使用四种不同的方法来确定一个字符串是否包含一个列表中的元素。此外,我们还学习了如何将结果显示为一个布尔值,而不是使用条件语句。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3