亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2023年06月14日 09:03:46   作者:跡憶客  
在數(shù)據(jù)預處理或糾錯的過程中可能會用到對列表中是否含有我們需要的字符串的判斷,下面這篇文章主要給大家介紹了關于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')

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

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

我們使用生成器表達式來迭代字符串集合。

生成器表達式用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。

在示例中,我們遍歷字符串列表并檢查字符串中是否包含每個子字符串。

any 函數(shù)將一個可迭代對象作為參數(shù),如果可迭代對象中的任何元素為真,則返回 True。

如果字符串至少包含列表中的一個元素,any() 函數(shù)將短路返回 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() 函數(shù)的 iterable 為空或 iterable 中的元素都不為真,則 any 函數(shù)返回 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']

列表推導用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。

在每次迭代中,我們檢查是否在字符串中找到當前子字符串并返回結果。

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

以不區(qū)分大小寫的方式檢查

如果我們需要以不區(qū)分大小寫的方式檢查字符串是否包含列表中的元素,請將兩個字符串都轉換為小寫。

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 方法返回字符串的副本,其中所有大小寫字符都轉換為小寫。

要執(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)遍歷字符串列表。

在每次迭代中,我們檢查當前子字符串是否包含在字符串中。

如果滿足條件,我們將 is_contained 變量設置為 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)  # ??? 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')

我們使用生成器表達式來遍歷列表。

生成器表達式用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。

在每次迭代中,我們檢查子字符串是否包含在當前列表項中并返回結果。

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 函數(shù)將一個可迭代對象作為參數(shù),如果可迭代對象中的任何元素為真,則返回 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 中是不可變的。

我們可以通過將兩個字符串都轉換為小寫或大寫來執(zhí)行不區(qū)分大小寫的成員資格測試。

找包含子字符串的列表項

如果我們需要查找包含子字符串的列表項,請使用列表推導。

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

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

列表推導用于對每個元素執(zhí)行某些操作或選擇滿足條件的元素子集。

新列表僅包含包含子字符串的列表項。

如果我們需要執(zhí)行不區(qū)分大小寫的成員資格測試,請將兩個字符串都轉換為小寫。

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 運算符來檢查當前項是否包含子字符串。

如果滿足條件,我們將 any_element_contains 變量設置為 True 并退出循環(huán)。

總結

到此這篇關于Python中如何檢查字符串是否包含列表中的元素的文章就介紹到這了,更多相關Python檢查字符串包含列表的元素內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python matplotlib學習筆記之坐標軸范圍

    Python matplotlib學習筆記之坐標軸范圍

    這篇文章主要介紹了Python matplotlib學習筆記之坐標軸范圍,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-06-06
  • python數(shù)據(jù)庫PooledDB連接池初始化使用示例

    python數(shù)據(jù)庫PooledDB連接池初始化使用示例

    這篇文章主要為大家介紹了python數(shù)據(jù)庫PooledDB連接池初始化使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-08-08
  • Python利用tenacity庫處理超時重試機制詳解

    Python利用tenacity庫處理超時重試機制詳解

    Python?的?tenacity?庫用于實現(xiàn)重試機制,特別適合處理網(wǎng)絡不穩(wěn)定或其他意外錯誤導致的函數(shù)調用失敗,下面我們就來看看它的具體使用吧
    2025-02-02
  • 解鎖Python并發(fā)編程中多線程與多進程的應用

    解鎖Python并發(fā)編程中多線程與多進程的應用

    本文我們將先從基本概念開始,然后通過詳細舉例探討每一種機制,特別關注多線程和多進程的應用,最后分享一些實戰(zhàn)經(jīng)驗以及一種優(yōu)雅的編程技巧,希望對大家有所幫助
    2023-05-05
  • python基礎教程之自定義函數(shù)介紹

    python基礎教程之自定義函數(shù)介紹

    這篇文章主要介紹了python基礎教程之自定義函數(shù)介紹,本文講解了python中函數(shù)的定義方法、函數(shù)參數(shù)的定義方法,需要的朋友可以參考下
    2014-08-08
  • Python?LeNet網(wǎng)絡詳解及pytorch實現(xiàn)

    Python?LeNet網(wǎng)絡詳解及pytorch實現(xiàn)

    LeNet主要用來進行手寫字符的識別與分類,并在美國的銀行中投入了使用。本文主要為大家詳細介紹了LetNet以及通過pytorch實現(xiàn)LetNet,感興趣的小伙伴可以學習一下
    2021-11-11
  • django DRF圖片路徑問題的解決方法

    django DRF圖片路徑問題的解決方法

    這篇文章主要給大家介紹了關于django DRF圖片路徑問題的解決方法,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • Python如何輸出百分比

    Python如何輸出百分比

    這篇文章主要介紹了Python 如何輸出百分比,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • Python 程序報錯崩潰后如何倒回到崩潰的位置(推薦)

    Python 程序報錯崩潰后如何倒回到崩潰的位置(推薦)

    這篇文章主要介紹了Python 程序報錯崩潰后如何倒回到崩潰的位置,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Pytorch mask_select 函數(shù)的用法詳解

    Pytorch mask_select 函數(shù)的用法詳解

    今天小編就為大家分享一篇Pytorch mask_select 函數(shù)的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02

最新評論