簡體   English   中英

從列表中查找與給定幾個字母匹配的所有字符串

[英]find all strings from list which matches with given few alphabets

我正在構建一個簡單的函數來搜索列表中的一些數據。 我正在嘗試從列表中搜索字符串與start of string某些字符匹配。

喜歡 :-

my_list = ["com truise", "bill james", "bill bates", "bustin jeiber"]

def search_from_list(searched_word):
    for word in my_list:
        if searched_word == word:
            print("Matched")
            print("All words" + __full_word_that_matched__)

search_from_list("bi")

如果用戶使用參數"bi"調用該函數,那么我將嘗試獲取與該字符串"bi"匹配的字符串,例如:-

["bill james", "bill bates"]

但這不起作用,我嘗試了很多方法,例如:-

def search_from_list(searched_word):
    my_list = ["com truise", "bill james", "bill bates", "bustin jeiber"]

    print re.findall(r"(?=("+'|'.join(my_list)+r"))", searched_word)

searched_word("bi")

但它返回空

然后我嘗試了

def search_from_list(searched_word):
    words = re.findall(r'\w+', searched_word)
    return [word for word in words if word in my_list]

n = search_from_list("bi")

它還向我顯示了空列表

任何幫助將非常感激。 先感謝您

my_list = ...

def search_from_list(substring):
    return [string for string in my_list if substring in string]

用正則表達式

import re
my_list = ["com truise", "bill james", "bill bates", "bustin jeiber"]

def search_from_list(searched_word):
    return [word for word in my_list if re.match(searched_word, word)]

print(search_from_list("bi"))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM