Python中如何检查字符串是否包含列表中的元素

 更新时间:2023年06月14日 09:03:46   作者:迹忆客  
在数据预处理或纠错的过程中可能会用到对列表中是否含有我们需要的字符串的判断,下面这篇文章主要给大家介绍了关于Python中如何检查字符串是否包含列表中的元素的相关资料,需要的朋友可以参考下

使用 any() 函数检查字符串是否包含列表中的元素。

如果字符串至少包含列表中的一个元素,any() 函数将返回 True,否则返回 False。

my_str = 'one two three'
my_list = ['a', 'two', 'c']
if any(substring in my_str for substring in my_list):
    # 👇️ this runs
    print('The string contains at least one element from the list')
else:
    print('The string does NOT contain any of the elements in the list')

如果需要检查列表中的任何元素是否包含字符串,可以看以下方式:

  • 检查列表中的任何元素是否包含字符串

我们使用生成器表达式来迭代字符串集合。

生成器表达式用于对每个元素执行某些操作或选择满足条件的元素子集。

在示例中,我们遍历字符串列表并检查字符串中是否包含每个子字符串。

any 函数将一个可迭代对象作为参数,如果可迭代对象中的任何元素为真,则返回 True。

如果字符串至少包含列表中的一个元素,any() 函数将短路返回 True。

in 运算符测试成员资格。 例如,如果 x 是 s 的成员,则 x in s 的计算结果为 True,否则计算结果为 False。

获取匹配的子串

如果需要获取字符串中包含的列表项,可以使用赋值表达式语法。

my_str = 'one two three'
my_list = ['a', 'two', 'c']
if any((match := substring) in my_str for substring in my_list):
    # 👇️ this runs
    print('The string contains at least one element from the list')
    print(match)  # 👉️ 'two'
else:
    print('The string does NOT contain any of the elements in the list')

赋值表达式允许我们使用 NAME := 表达式语法为表达式中的变量赋值。

如果我们传递给 any() 函数的 iterable 为空或 iterable 中的元素都不为真,则 any 函数返回 False。

获取所有匹配的子串

如果我们需要获取所有匹配的子字符串,请使用列表推导。

my_str = 'one two three'
my_list = ['a', 'two', 'c', 'one']
matches = [substring for substring in my_list
           if substring in my_str]
print(matches) # 👉️ ['two', 'one']

列表推导用于对每个元素执行某些操作或选择满足条件的元素子集。

在每次迭代中,我们检查是否在字符串中找到当前子字符串并返回结果。

新列表包含字符串中包含的所有列表元素。

以不区分大小写的方式检查

如果我们需要以不区分大小写的方式检查字符串是否包含列表中的元素,请将两个字符串都转换为小写。

my_str = 'ONE TWO THREE'
my_list = ['a', 'two', 'c']
if any(substring.lower() in my_str.lower() for substring in my_list):
    # 👇️ this runs
    print('The string contains at least one element from the list')
else:
    print('The string does NOT contain any of the elements in the list')

在检查每个列表项是否包含在字符串中之前,我们使用 str.lower() 方法将每个列表项和字符串转换为小写。

str.lower 方法返回字符串的副本,其中所有大小写字符都转换为小写。

要执行不区分大小写的比较,两个字符串都必须是小写或大写。

使用 for 循环检查字符串是否包含列表中的元素

我们还可以使用 for 循环来检查字符串是否包含列表中的元素。

my_str = 'one two three'
my_list = ['a', 'two', 'c']
is_contained = False
for substring in my_list:
    if substring in my_str:
        is_contained = True
        break
print(is_contained)  # 👉️ True
if is_contained:
    # 👇️ this runs
    print('The string contains at least one element from the list')
else:
    print('The string does NOT contain any of the elements in the list')

我们将 is_contained 变量初始化为 False 并使用 for 循环遍历字符串列表。

在每次迭代中,我们检查当前子字符串是否包含在字符串中。

如果满足条件,我们将 is_contained 变量设置为 True 并退出 for 循环。

检查 List 中是否有任何元素包含 String

检查列表中的任何元素是否包含字符串:

  • 使用生成器表达式迭代列表。
  • 使用 in 运算符检查字符串是否包含在每个列表项中。
  • 如果条件至少满足一次,则该字符串包含在列表中。
my_list = ['fql', 'jiyik', 'com']
substring = 'z'
result = any(substring in word for word in my_list)
print(result)  # 👉️ True
if any(substring in word for word in my_list):
    # 👇️ this runs
    print('The substring is contained in at least one of the list items')
else:
    print('The substring is NOT contained in any of the list items')

我们使用生成器表达式来遍历列表。

生成器表达式用于对每个元素执行某些操作或选择满足条件的元素子集。

在每次迭代中,我们检查子字符串是否包含在当前列表项中并返回结果。

my_list = ['fql', 'jiyik', 'com']
substring = 'z'

result = any(substring in word for word in my_list)
print(result)  # 👉️ True

in 运算符测试成员资格。 例如,如果 x 是 s 的成员,则 x in s 的计算结果为 True,否则计算结果为 False。

my_str = 'fql jiyik'

print('fql' in my_str)  # 👉️ True
print('another' in my_str)  # 👉️ False

x not in s 返回 x in s 的否定。

any 函数将一个可迭代对象作为参数,如果可迭代对象中的任何元素为真,则返回 True。

检查列表中的任何元素是否包含字符串,忽略大小写

如果我们需要检查某个子字符串是否包含在忽略大小写的列表中的任何项目中,请将两个字符串都转换为小写。

my_list = ['FQL', 'JIYIK', 'COM']
substring = 'z'

result = any(substring.lower() in word.lower() for word in my_list)
print(result)  # 👉️ True

if any(substring.lower() in word.lower() for word in my_list):
    # 👇️ this runs
    print('The substring is contained in at least one of the list items')
else:
    print('The substring is NOT contained in any of the list items')

str.lower 方法返回字符串的副本,其中所有大小写字符都转换为小写。

该方法不会更改原始字符串,它会返回一个新字符串。 字符串在 Python 中是不可变的。

我们可以通过将两个字符串都转换为小写或大写来执行不区分大小写的成员资格测试。

找包含子字符串的列表项

如果我们需要查找包含子字符串的列表项,请使用列表推导。

my_list = ['fql', 'jiyik', 'com']
substring = 'k'

matches = [word for word in my_list if substring in word]
print(matches)  # 👉️ ['jiyik']

列表推导用于对每个元素执行某些操作或选择满足条件的元素子集。

新列表仅包含包含子字符串的列表项。

如果我们需要执行不区分大小写的成员资格测试,请将两个字符串都转换为小写。

my_list = ['fql', 'jiyik', 'com']
substring = 'K'
matches = [word for word in my_list if substring.lower() in word.lower()]
print(matches)  # 👉️ ['jiyik']

使用 for 循环检查列表中的任何元素是否包含字符串

我们还可以使用 for 循环来检查列表中的任何元素是否包含字符串。

my_list = ['fql', 'jiyik', 'com']
substring = 'k'

any_element_contains = False

for item in my_list:
    if substring in item:
        any_element_contains = True
        break

print(any_element_contains)  # 👉️ True

if any_element_contains:
    # 👇️ this runs
    print('The substring is contained in at least one of the list items')
else:
    print('The substring is NOT contained in any of the list items')

我们将 any_element_contains 变量初始化为 False。

在每次迭代中,我们使用 in 运算符来检查当前项是否包含子字符串。

如果满足条件,我们将 any_element_contains 变量设置为 True 并退出循环。

总结

到此这篇关于Python中如何检查字符串是否包含列表中的元素的文章就介绍到这了,更多相关Python检查字符串包含列表的元素内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python中的Numeric包和Numarray包使用教程

    Python中的Numeric包和Numarray包使用教程

    这篇文章主要介绍了Python中的Numeric包和Numarray包使用教程,来自IBM官方网站上的技术文档,需要的朋友可以参考下
    2015-04-04
  • Python中join()函数多种操作代码实例

    Python中join()函数多种操作代码实例

    这篇文章主要介绍了Python中join()函数多种操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • pandas高效读取大文件的示例详解

    pandas高效读取大文件的示例详解

    使用 pandas 进行数据分析时,第一步就是读取文件,所以这篇文章主要来和大家讨论一下pandas如何高效读取大文件,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下
    2024-01-01
  • Python办公自动化Word转Excel文件批量处理

    Python办公自动化Word转Excel文件批量处理

    这篇文章主要为大家介绍了Python办公自动化Word转Excel文件批量处理示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • Django 解决新建表删除后无法重新创建等问题

    Django 解决新建表删除后无法重新创建等问题

    这篇文章主要介绍了Django 解决新建表删除后无法重新创建等问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • 浅析PyCharm 的初始设置(知道)

    浅析PyCharm 的初始设置(知道)

    这篇文章主要介绍了PyCharm 的初始设置(知道),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • python如何在文件中部插入信息

    python如何在文件中部插入信息

    这篇文章主要介绍了python如何在文件中部插入信息问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • python模块如何查看

    python模块如何查看

    在本篇文章中小编给大家整理的是关于python的模块位置的相关知识点,需要的朋友们可以参考下。
    2020-06-06
  • Python实现自动驾驶训练模型

    Python实现自动驾驶训练模型

    这篇文章主要为大家介绍了Python实现自动驾驶训练模型,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • python单例模式原理与创建方法实例分析

    python单例模式原理与创建方法实例分析

    这篇文章主要介绍了python单例模式原理与创建方法,结合实例形式分析了Python单例模式的概念、原理、定义、使用方法及相关操作注意事项,需要的朋友可以参考下
    2019-10-10

最新评论