Python中如何檢查字符串是否包含列表中的元素
使用 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')
如果需要檢查列表中的任何元素是否包含字符串,可以看以下方式:
- 檢查列表中的任何元素是否包含字符串
我們使用生成器表達(dá)式來迭代字符串集合。
生成器表達(dá)式用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。
在示例中,我們遍歷字符串列表并檢查字符串中是否包含每個子字符串。
any
函數(shù)將一個可迭代對象作為參數(shù),如果可迭代對象中的任何元素為真,則返回 True。
如果字符串至少包含列表中的一個元素,any()
函數(shù)將短路返回 True。
in
運(yùn)算符測試成員資格。 例如,如果 x 是 s 的成員,則 x in s 的計(jì)算結(jié)果為 True,否則計(jì)算結(jié)果為 False。
獲取匹配的子串
如果需要獲取字符串中包含的列表項(xiàng),可以使用賦值表達(dá)式語法。
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')
賦值表達(dá)式允許我們使用 NAME := 表達(dá)式語法為表達(dá)式中的變量賦值。
如果我們傳遞給 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')
在檢查每個列表項(xiàng)是否包含在字符串中之前,我們使用 str.lower()
方法將每個列表項(xiàng)和字符串轉(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
檢查列表中的任何元素是否包含字符串:
- 使用生成器表達(dá)式迭代列表。
- 使用 in 運(yùn)算符檢查字符串是否包含在每個列表項(xiàng)中。
- 如果條件至少滿足一次,則該字符串包含在列表中。
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')
我們使用生成器表達(dá)式來遍歷列表。
生成器表達(dá)式用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。
在每次迭代中,我們檢查子字符串是否包含在當(dāng)前列表項(xiàng)中并返回結(jié)果。
my_list = ['fql', 'jiyik', 'com'] substring = 'z' result = any(substring in word for word in my_list) print(result) # ??? True
in 運(yùn)算符測試成員資格。 例如,如果 x 是 s 的成員,則 x in s 的計(jì)算結(jié)果為 True,否則計(jì)算結(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。
檢查列表中的任何元素是否包含字符串,忽略大小寫
如果我們需要檢查某個子字符串是否包含在忽略大小寫的列表中的任何項(xiàng)目中,請將兩個字符串都轉(zhuǎn)換為小寫。
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
方法返回字符串的副本,其中所有大小寫字符都轉(zhuǎn)換為小寫。
該方法不會更改原始字符串,它會返回一個新字符串。 字符串在 Python 中是不可變的。
我們可以通過將兩個字符串都轉(zhuǎn)換為小寫或大寫來執(zhí)行不區(qū)分大小寫的成員資格測試。
找包含子字符串的列表項(xiàng)
如果我們需要查找包含子字符串的列表項(xiàng),請使用列表推導(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í)行某些操作或選擇滿足條件的元素子集。
新列表僅包含包含子字符串的列表項(xiàng)。
如果我們需要執(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 運(yùn)算符來檢查當(dāng)前項(xiàng)是否包含子字符串。
如果滿足條件,我們將 any_element_contains
變量設(shè)置為 True 并退出循環(huán)。
總結(jié)
到此這篇關(guān)于Python中如何檢查字符串是否包含列表中的元素的文章就介紹到這了,更多相關(guān)Python檢查字符串包含列表的元素內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python matplotlib學(xué)習(xí)筆記之坐標(biāo)軸范圍
這篇文章主要介紹了Python matplotlib學(xué)習(xí)筆記之坐標(biāo)軸范圍,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06python數(shù)據(jù)庫PooledDB連接池初始化使用示例
這篇文章主要為大家介紹了python數(shù)據(jù)庫PooledDB連接池初始化使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Python利用tenacity庫處理超時(shí)重試機(jī)制詳解
Python?的?tenacity?庫用于實(shí)現(xiàn)重試機(jī)制,特別適合處理網(wǎng)絡(luò)不穩(wěn)定或其他意外錯誤導(dǎo)致的函數(shù)調(diào)用失敗,下面我們就來看看它的具體使用吧2025-02-02解鎖Python并發(fā)編程中多線程與多進(jìn)程的應(yīng)用
本文我們將先從基本概念開始,然后通過詳細(xì)舉例探討每一種機(jī)制,特別關(guān)注多線程和多進(jìn)程的應(yīng)用,最后分享一些實(shí)戰(zhàn)經(jīng)驗(yàn)以及一種優(yōu)雅的編程技巧,希望對大家有所幫助2023-05-05Python?LeNet網(wǎng)絡(luò)詳解及pytorch實(shí)現(xiàn)
LeNet主要用來進(jìn)行手寫字符的識別與分類,并在美國的銀行中投入了使用。本文主要為大家詳細(xì)介紹了LetNet以及通過pytorch實(shí)現(xiàn)LetNet,感興趣的小伙伴可以學(xué)習(xí)一下2021-11-11Python 程序報(bào)錯崩潰后如何倒回到崩潰的位置(推薦)
這篇文章主要介紹了Python 程序報(bào)錯崩潰后如何倒回到崩潰的位置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Pytorch mask_select 函數(shù)的用法詳解
今天小編就為大家分享一篇Pytorch mask_select 函數(shù)的用法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02