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',所以答案是yes。
方法一:使用嵌套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
方法二:使用列表推导式
列表推导式
当你想基于现有列表的值构建一个新列表时,列表推导式提供了一种更短/简洁的语法。
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
方法三:使用any()函数
如果迭代器中的任何项目为真,则any()函数返回True,否则返回False。
语法
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
方法四:使用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
结论
在本文中,我们学习了四种不同的方法来确定字符串是否包含列表中的元素。此外,我们还学习了如何显示布尔结果,而不是使用条件语句。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP