I have a dictionary which uses a 4-tuple as it’s key. I need to find all the keys in the dictionary which partially match some other tuple. I have some code which does this but it’s slow and needs optimizing.
Here’s what I’m after:
Keys:
(1, 2, 3, 4)
(1, 3, 5, 2)
(2, 4, 8, 7)
(1, 4, 3, 4)
Match:
(1, None, 3, None)
Result:
[(1, 2, 3, 4), (1, 4, 3, 4)]
Current code:
def GetTuples(self, keyWords):
tuples = []
for k in self.chain.iterkeys():
match = True
for i in range(self.order):
if keyWords[i] is not None and keyWords[i] != k[i]:
match = False
break
if match is True:
tuples.append(k)
return tuples
- keyWords is a list containing the values I want to match
- self.chain is the dictionary
- self.order is the size of the tuple
- len(keyWords) always = len(k)
- ‘None’ is considered the wild card
- The dictionary is pretty huge (this method is taking ~800ms to run and about 300mb), so space is also a consideration
I’m basically looking for either optimizations to this method, or a better way of storing this data.
What about just using a database?
I prefer SQLite + SQLAlchemy even for simple projects, but plain sqlite3 might have a gentler learning curve.
Putting an index on each key column should take care of speed issues.