Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Find a Partial String in a Python List?
1
How to Find a Partial String in a Python List?

5/5 – (1 vote)

Problem Formulation

[Image: 1f4ac.png] Challenge: Given a Python list of strings and a query string. Find the strings that partially match the query string.

Example 1:
  • Input:
    Code:
    ['hello', 'world', 'python']
    and
    Code:
    'pyth'

  • Output:
    Code:
    ['python']

Example 2:
  • Input:
    Code:
    ['aaa', 'aa', 'a']
    and
    Code:
    'a'

  • Output:
    Code:
    ['aaa', 'aa', 'a']

Example 3:
  • Input:
    Code:
    ['aaa', 'aa', 'a']
    and
    Code:
    'b'

  • Output:
    Code:
    []

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:
query
in a string list
Code:
lst
is to use the membership operator
Code:
in
and the list comprehension statement like so:
Code:
[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.

[Image: 1f449.png] Recommended Tutorial: List Comprehension in Python

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

[Image: 1f449.png] Recommended Tutorial: The Membership Operator in Python

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Method 2: list() and filter()

To find a list of partial query matches given a string list
Code:
lst
, combine the membership operator with the
Code:
filter()
function in which you pass a lambda function that evaluates the membership operation for each element in the list like so:
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? [Image: 1f984.png]

I recommend you check out the following tutorial with video to shed some light on the background information here:

[Image: 1f449.png] Recommended Tutorial: Python Filtering

[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Generally, I like list comprehension more than the
Code:
filter()
function because the former is more concise (e.g., no need to convert the result to a list) and slightly faster. But both work perfectly fine!

Method 3: Regex Match + List Comprehension

The most flexible way to find a list of partial query matches given a string list
Code:
lst
is provided by Python’s powerful regular expressions functionality. For example, the expression
Code:
[x for x in lst if re.match(pattern, x)]
finds all strings that match a certain query pattern as defined by you.

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.*
that simply matches words that contain the
Code:
query
string. However, you could also do more advanced pattern matching—regex to the rescue!

Again, I’d recommend you check out the background info on regular expressions:

[Image: 1f449.png] Recommended Tutorial: Python Regex
Code:
match()
— A Simple Illustrated Guide


[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]

Regex Humor

[Image: image-133.png][i]Wait, forgot to escape a space. Wheeeeee[taptaptap]eeeeee. (source)[/i]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: