Home > Software engineering >  Finding all values in an array that match a given string with placeholder characters?
Finding all values in an array that match a given string with placeholder characters?

Time:09-28

I have an array of strings, and when the user enters a string with question marks replacing some characters, I want the program to return all words from the array that it could be.

possibleWords = ["Animal", "Basket", "Bridge", "Guitar", "Needle", "Office", "Orange"]

#and the user enters "O????e", the program would return "Office" and "Orange"

CodePudding user response:

Try this in one line using list comprehension and all():

possibleWords = ["Animal", "Basket", "Bridge", "Guitar", "Needle", "Office", "Orange"]
m = "O????e"

result = [i for i in possibleWords if all(k=='?' or j==k  for j,k in zip(i,m))]

the output(result) will be:

In [4]: [i for i in possibleWords if all(k=='?' or j==k  for j,k in zip(i,m))]
Out[4]: ['Office', 'Orange']

also you can read about all() here.

Note that this will not check the length of the words, I assume that the input length is equal to the all of items in the list. but if not, you can use zip_logest from itertools like this:

from itertools import zip_longest

result = [i for i in possibleWords if all(k=='?' or j==k  for j,k in zip_longest(i,m))]

CodePudding user response:

The regex character you're looking for is ".", instead of "?". So by simple replacing the question marks with dots, you can use the regex module re to get your desired output:

import re

possibleWords = ["Animal", "Basket", "Bridge", "Guitar", "Needle", "Office", "Orange"]
entry = "O????e"

pattern = entry.replace("?", ".")
r = re.compile(pattern)
l = list(filter(r.match, possibleWords))

print(l)

Out:

['Office', 'Orange']
  • Related