Python 检测字符串是否包含列表中的元素
在本文中,我们将学习如何在Python中检查字符串是否包含列表中的元素。
使用的方法
- 使用嵌套的for循环
-
使用列表推导式
-
使用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() 函数(将字符串分割成列表。我们可以定义分隔符;默认分隔符是任何空格)将输入字符串分割成单词列表。
-
用0初始化临时标志(temp_flag)变量。
-
使用 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:使用列表推导
列表推导
当您希望基于现有列表的值构建一个新的列表时,列表推导提供了一种更短、更简洁的语法。
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()函数返回 True ,如果可迭代对象中的任何一个为真,否则返回False。
语法
any(iterable)
示例
以下程序使用任何()函数检查输入字符串是否包含任何输入列表元素 –
# 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
结论
在本文中,我们学习了如何使用四种不同的方法来确定一个字符串是否包含列表中的元素。此外,我们还学习了如何将结果显示为布尔值,而不是使用条件语句。