使用 any()
函數(shù)檢查字符串是否包含列表中的元素。
如果字符串至少包含列表中的一個元素,any()
函數(shù)將返回 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')
如果需要檢查列表中的任何元素是否包含字符串,可以看以下方式:
- 檢查列表中的任何元素是否包含字符串
我們使用生成器表達式來迭代字符串集合。
生成器表達式用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。
在示例中,我們遍歷字符串列表并檢查字符串中是否包含每個子字符串。
any
函數(shù)將一個可迭代對象作為參數(shù),如果可迭代對象中的任何元素為真,則返回 True。
如果字符串至少包含列表中的一個元素,any()
函數(shù)將短路返回 True。
in
運算符測試成員資格。 例如,如果 x 是 s 的成員,則 x in s 的計算結(jié)果為 True,否則計算結(jié)果為 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()
函數(shù)的 iterable 為空或 iterable 中的元素都不為真,則 any
函數(shù)返回 False。
獲取所有匹配的子串
如果我們需要獲取所有匹配的子字符串,請使用列表推導(dǎo)。
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']
列表推導(dǎo)用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。
在每次迭代中,我們檢查是否在字符串中找到當(dāng)前子字符串并返回結(jié)果。
新列表包含字符串中包含的所有列表元素。
以不區(qū)分大小寫的方式檢查
如果我們需要以不區(qū)分大小寫的方式檢查字符串是否包含列表中的元素,請將兩個字符串都轉(zhuǎn)換為小寫。
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()
方法將每個列表項和字符串轉(zhuǎn)換為小寫。
str.lower
方法返回字符串的副本,其中所有大小寫字符都轉(zhuǎn)換為小寫。
要執(zhí)行不區(qū)分大小寫的比較,兩個字符串都必須是小寫或大寫。
使用 for 循環(huán)檢查字符串是否包含列表中的元素
我們還可以使用 for 循環(huán)來檢查字符串是否包含列表中的元素。
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 循環(huán)遍歷字符串列表。
在每次迭代中,我們檢查當(dāng)前子字符串是否包含在字符串中。
如果滿足條件,我們將 is_contained
變量設(shè)置為 True 并退出 for 循環(huán)。
檢查 List 中是否有任何元素包含 String
檢查列表中的任何元素是否包含字符串:
- 使用生成器表達式迭代列表。
- 使用 in 運算符檢查字符串是否包含在每個列表項中。
- 如果條件至少滿足一次,則該字符串包含在列表中。
my_list = ['fql', 'jiyik', 'com']
substring = 'z'
result = any(substring in word for word in my_list)
print(result) # ??? False
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')
我們使用生成器表達式來遍歷列表。
生成器表達式用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。
在每次迭代中,我們檢查子字符串是否包含在當(dāng)前列表項中并返回結(jié)果。
my_list = ['fql', 'jiyik', 'com']
substring = 'z'
result = any(substring in word for word in my_list)
print(result) # ??? False
in 運算符測試成員資格。 例如,如果 x 是 s 的成員,則 x in s 的計算結(jié)果為 True,否則計算結(jié)果為 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
函數(shù)將一個可迭代對象作為參數(shù),如果可迭代對象中的任何元素為真,則返回 True。
檢查列表中的任何元素是否包含字符串,忽略大小寫
如果我們需要檢查某個子字符串是否包含在忽略大小寫的列表中的任何項目中,請將兩個字符串都轉(zhuǎn)換為小寫。
my_list = ['FQL', 'JIYIK', 'COM']
substring = 'z'
result = any(substring.lower() in word.lower() for word in my_list)
print(result) # ??? False
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
方法返回字符串的副本,其中所有大小寫字符都轉(zhuǎn)換為小寫。
該方法不會更改原始字符串,它會返回一個新字符串。 字符串在 Python 中是不可變的。
我們可以通過將兩個字符串都轉(zhuǎn)換為小寫或大寫來執(zhí)行不區(qū)分大小寫的成員資格測試。
找包含子字符串的列表項
如果我們需要查找包含子字符串的列表項,請使用列表推導(dǎo)。
my_list = ['fql', 'jiyik', 'com']
substring = 'k'
matches = [word for word in my_list if substring in word]
print(matches) # ??? ['jiyik']
列表推導(dǎo)用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。
新列表僅包含包含子字符串的列表項。
如果我們需要執(zhí)行不區(qū)分大小寫的成員資格測試,請將兩個字符串都轉(zhuǎn)換為小寫。
my_list = ['fql', 'jiyik', 'com']
substring = 'K'
matches = [word for word in my_list if substring.lower() in word.lower()]
print(matches) # ??? ['jiyik']
使用 for 循環(huán)檢查列表中的任何元素是否包含字符串
我們還可以使用 for 循環(huán)來檢查列表中的任何元素是否包含字符串。
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 運算符來檢查當(dāng)前項是否包含子字符串。文章來源:http://www.zghlxwxcb.cn/news/detail-486145.html
如果滿足條件,我們將 any_element_contains
變量設(shè)置為 True 并退出循環(huán)。文章來源地址http://www.zghlxwxcb.cn/news/detail-486145.html
到了這里,關(guān)于Python 中檢查字符串是否包含列表中的元素的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!