Python, accessing dictionary with wildcards
dictionary, python, regex
Solution
You can create a class that will do that for you that wraps the dictionary class:
import re
class RegexDict(dict):
def get_matching(self, event):
return (self[key] for key in self if re.match(key, event))
Example of use:
>>> d = {'a.b': 'item1', 'b.c': 'item2'}
>>> rd = RegexDict(d)
>>> for o in rd.get_matching('a1b'):
print o
item1
This saves you having to rely on external functions, it keeps the overhead small, and is more robust.
You could also add another function that takes a list (or iter) as input and returns all matching values:
def get_all_matching(self, events):
return (match for event in events for match in self.get_matching(event))
>>> for o in rd.get_all_matching(['a1b', 'b2c']):
print o
item1
item2
Problem
I have a dictionary with items, where the keys are some kind of regular expressions. I am looking for a function to return a list of matching items whenever a matching string is passed ``` d = {'a.b': item1, 'b.c':item2} func(d,'a1b') >>> [item1] func(d,'b2c') >>> [item2] ``` Is there a pythonic way to do this ? The only solution I can come up with is: ``` import re def func(d, event): res = list() for key, item in d.iteritems(): if re.match(key, event): res.append(item) return res ```