I'd recommend converting ['A', 'B', '*', 'D']
to '^AB.*D$'
, ['A', 'B', 'C', 'C', 'C', 'D']
to 'ABCCCD'
, and then using the re
module (regular expressions) to do the match.
This will be valid if the elements of your lists are only one character each, and if they're strings.
something like:
import(re)
def myMatch( patternList, stringList ):
# convert pattern to flat string with wildcards
# convert AB*D to valid regex ^AB.*D$
pattern = ''.join(patternList)
regexPattern = '^' + pattern.replace('*','.*') + '$'
# perform matching
against = ''.join(stringList) # convert ['A','B','C','C','D'] to ABCCCD
# return whether there is a match
return (re.match(regexPattern,against) is not None)
If the lists contain numbers, or words, choose a character that you wouldn't expect to be in either, for example #
. Then ['Aa','Bs','Ce','Cc','CC','Dd']
can be converted to Aa#Bs#Ce#Cc#CC#Dd
, the wildcard pattern ['Aa','Bs','*','Dd']
could be converted to ^Aa#Bs#.*#Dd$
, and the match performed.
Practically speaking this just means all the ''.join(...)
becomes '#'.join(...)
in myMatch
.