So, I have a list of regex patterns, and a list of strings, what I want to do is to say within this list of strings, are there any strings which do not match any of the regexes.
At present, I'm pulling out the regexes, and the values to be matched by the regex from two dictionaries:
I've made two lists, one of patterns, one of keys, from two dictionaries:
patterns = []
keys = []
for pattern, schema in patternproperties.items():
patterns.append(pattern)
for key, value in value_obj.items():
keys.append(key)
# Now work out if there are any non-matching keys
for key in keys:
matches = 0
for pattern in patterns:
if re.match(pattern, key):
matches += 1
if matches == 0:
print 'Key %s matches no patterns' %(key)
But this seems horribly inefficient. Anyone have any pointers to a better solution to this?
re.match? search() vs. match()patternslist is completely useless. Simply iterate overpatternpropertiesdictionary.for pattern, schema in patternproperties.items(): patterns.append(pattern)does exactly the same thing aspatterns = patternproperties.keys(), just less obviously, more verbosely, and probably slower to boot. And likewise forkeys. It's justvalue_obj.keys(). And, as Bakuriu points out, looping over a dictionary is the same as looping over its keys.