如何在 Python 中檢查字串是否以列表中的某個字尾結尾?


字尾是指新增到單詞末尾的一個字母或一組字母,以構成一個新單詞。

假設您有一系列字尾,它們是可以新增到單詞末尾以改變其含義的字母組合。您希望在 Python 中檢查給定的字串是否以這些字尾之一結尾。

檢查字串是否以列表中的字尾結尾

示例

在此示例中,我們首先定義一個字尾列表,我們希望檢查字串是否以其結尾。然後,我們使用 input() 函式提示使用者輸入一個字串。

然後,我們使用 for 迴圈遍歷字尾列表中的每個字尾。我們使用 endswith() 方法檢查輸入字串是否以迴圈中的當前後綴結尾。

如果輸入字串以當前字尾結尾,我們列印一條訊息指示其以哪個字尾結尾,並使用 break 語句退出迴圈。

如果列表中的任何字尾都不匹配輸入字串的結尾,我們列印一條訊息指示該字串不以任何字尾結尾。

suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

for suffix in suffixes:
    if input_str.endswith(suffix):
        print(f"The string ends with {suffix}")
        break
else:
    print("The string does not end with any of the suffixes")

輸出

Enter a string: Wanted
The string ends with ed

使用列表推導式

示例

在此示例中,我們使用列表推導式建立一個輸入字串結尾的字尾列表。我們遍歷字尾列表中的每個字尾,並使用 endswith() 方法檢查輸入字串是否以當前字尾結尾。

然後,我們使用 if 語句檢查結果列表是否不為空。如果列表不為空,我們列印一條訊息指示字串以哪個字尾結尾。如果列表為空,我們列印一條訊息指示字串不以任何字尾結尾。

suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

result = [suffix for suffix in suffixes if input_str.endswith(suffix)]

if result:
    print(f"The string ends with {result[0]}")
else:
    print("The string does not end with any of the suffixes")

輸出

Enter a string: Slowly
The string ends with ly

使用 any() 函式

示例

在此示例中,我們使用 any() 函式檢查輸入字串是否以後綴列表中的任何字尾結尾。我們將一個生成器表示式傳遞給 any() 函式,該表示式遍歷列表中的每個字尾並檢查輸入字串是否以該字尾結尾。

如果任何字尾與輸入字串的結尾匹配,我們列印一條訊息指示該字串以列表中的某個字尾結尾。如果沒有任何字尾與輸入字串的結尾匹配,我們列印一條訊息指示該字串不以任何字尾結尾。

suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

if any(input_str.endswith(suffix) for suffix in suffixes):
    print(f"The string ends with one of the suffixes in {suffixes}")
else:
    print("The string does not end with any of the suffixes")

輸出

Enter a string: Monalisa
The string does not end with any of the suffixes

使用 filter() 函式

示例

在此示例中,我們使用 filter() 函式建立一個新列表,該列表僅包含輸入字串結尾的字尾。我們將 endswith() 方法作為過濾器函式,並將字尾列表作為要過濾的可迭代物件。

然後,我們使用 list() 函式將過濾後的字尾轉換為列表,並將其賦值給 result 變數。

如果 result 列表不為空,我們列印一條訊息指示字串以哪個字尾結尾。如果 result 列表為空,我們列印一條訊息指示字串不以任何字尾結尾。

suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

filtered_suffixes = filter(input_str.endswith, suffixes)
result = list(filtered_suffixes)

if result:
    print(f"The string ends with {result[0]}")
else:
    print("The string does not end with any of the suffixes")

輸出

Enter a string: Surfing
The string ends with ing

使用正則表示式

示例

在此示例中,我們使用正則表示式檢查輸入字串是否以後綴列表中的任何字尾結尾。我們首先匯入 re 模組,該模組為 Python 中的正則表示式提供支援。

然後,我們定義一個正則表示式模式,該模式匹配列表中字尾之前的任何字元,並以其中一個字尾結尾。我們使用格式化字串字面量 (f-string) 根據列表中的字尾動態建立正則表示式模式。

然後,我們使用 re.match() 函式檢查輸入字串是否與正則表示式模式匹配。如果輸入字串與模式匹配,我們列印一條訊息指示該字串

import re
suffixes = ['ing', 'ed', 'ly']
input_str = input("Enter a string: ")

regex_pattern = fr".*({'|'.join(suffixes)})$"

if re.match(regex_pattern, input_str):
    print(f"The string ends with one of the suffixes in {suffixes}")
else:
    print("The string does not end with any of the suffixes")

輸出

Enter a string: Lily
The string ends with one of the suffixes in ['ing', 'ed', 'ly']

更新於: 2023年8月10日

581 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.