![]() |
|
[Tut] How to Find a Partial String in a Python List? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Find a Partial String in a Python List? (/thread-99938.html) |
[Tut] How to Find a Partial String in a Python List? - xSicKxBot - 09-12-2022 How to Find a Partial String in a Python List? 5/5 – (1 vote) Problem Formulation Challenge: Given a Python list of strings and a query string. Find the strings that partially match the query string.Example 1:
Example 2:
Example 3:
Let’s dive into several methods that solve this and similar type of problems. We start with the most straightforward solution. Method 1: Membership + List Comprehension The most Pythonic way to find a list of partial matches of a given string Code: queryCode: lstCode: inCode: [s for s in lst if query in s]Here’s a simple example: Code: def partial(lst, query): return [s for s in lst if query in s] # Example 1:
print(partial(['hello', 'world', 'python'], 'pyth'))
# ['python'] # Example 2:
print(partial(['aaa', 'aa', 'a'], 'a'))
# ['aaa', 'aa', 'a'] # Example 3:
print(partial(['aaa', 'aa', 'a'], 'b'))
# []In case you need some background information, feel free to check out our two tutorials and the referenced videos. Recommended Tutorial: List Comprehension in Python Recommended Tutorial: The Membership Operator in PythonMethod 2: list() and filter() To find a list of partial query matches given a string list Code: lstCode: filter()Code: list(filter(lambda x: query in x, lst))Here’s an example: Code: def partial(lst, query): return list(filter(lambda x: query in x, lst)) # Example 1:
print(partial(['hello', 'world', 'python'], 'pyth'))
# ['python'] # Example 2:
print(partial(['aaa', 'aa', 'a'], 'a'))
# ['aaa', 'aa', 'a'] # Example 3:
print(partial(['aaa', 'aa', 'a'], 'b'))
# []Beautiful Python one-liner, isn’t it? ![]() I recommend you check out the following tutorial with video to shed some light on the background information here: Recommended Tutorial: Python FilteringGenerally, I like list comprehension more than the Code: filter()Method 3: Regex Match + List Comprehension The most flexible way to find a list of partial query matches given a string list Code: lstCode: [x for x in lst if re.match(pattern, x)]The following examples showcase this solution: Code: import re def partial(lst, query): pattern = '.*' + query + '.*' return [x for x in lst if re.match(pattern, x)] # Example 1:
print(partial(['hello', 'world', 'python'], 'pyth'))
# ['python'] # Example 2:
print(partial(['aaa', 'aa', 'a'], 'a'))
# ['aaa', 'aa', 'a'] # Example 3:
print(partial(['aaa', 'aa', 'a'], 'b'))
# []In this example, we use the dummy pattern Code: .*query.*Code: queryAgain, I’d recommend you check out the background info on regular expressions: Recommended Tutorial: Python Regex Code: match()Regex Humor [i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
|