I have a tuple of tuples and a tuple. I’m interested to know which elements of the first tuple match the second tuple (if any), considering partial matches too.
This is a filter function to demonstrate what I mean.
def f(repo):
pattern = (None, None, '1.3')
for idx, item in enumerate(pattern):
if item != None and item != repo[idx]:
return False
return True
>>> repo = (('framework', 'django', '1.3'), ('cms', 'fein', '1.3'), ('cms', 'django-cms', '2.2'))
>>> filter(f, repo)
(('framework', 'django', '1.3'), ('cms', 'fein', '1.3'))
The filter is useless in this form because the pattern can’t be provided externally as an argument (I want to use the same function to check different inputs). Is there a way to fix this?
And, what could be another algorithm to embrace for a better approach to the original problem?
You can use a closure to bind the pattern into the function:
I’ve also provided a different expression for comparing the tuples.