Python dict key delete, if key pattern match with other dict key.
e.g.
a={'a.b.c.test':1, 'b.x.d.pqr':2, 'c.e.f.dummy':3, 'd.x.y.temp':4}
b={'a.b.c':1, 'b.p.q':20}
result
a={'b.x.d.pqr':2,'c.e.f.dummy':3,'d.x.y.temp':4}`
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If “pattern match with other dict key” means “starts with any key in the other dict”, the most direct way to write that would be like this:
If that’s hard to follow at first glance, it’s basically the equivalent of this:
This is going to be slower than necessary. If
ahas N keys, andbhas M keys, the time taken is O(NM). While you can checked “does keykexist in dictb” in constant time, there’s no way to check “does any key starting withkexist in dictb” without iterating over the whole dict. So, ifbis potentially large, you probably want to searchsorted(b.keys())and write a binary search, which will get the time down to O(N log M). But if this isn’t a bottleneck, you may be better off sticking with the simple version, just because it’s simple.Note that I’m generating a new
awith the matches filtered out, rather than deleting the matches. This is almost always a better solution than deleting in-place, for multiple reasons:* It’s much easier to reason about. Treating objects as immutable and doing pure operations on them means you don’t need to think about how states change over time. For example, the naive way to delete in place would run into the problem that you’re changing the dictionary while iterating over it, which will raise an exception. Issues like that never come up without mutable operations.
* It’s easier to read, and (once you get the hang of it) even to write.
* It’s almost always faster. (One reason is that it takes a lot more memory allocations and deallocations to repeatedly modify a dictionary than to build one with a comprehension.)
The one tradeoff is memory usage. The delete-in-place implementation has to make a copy of all of the keys; the built-a-new-dict implementation has to have both the filtered dict and the original dict in memory. If you’re keeping 99% of the values, and the values are much larger than the keys, this could hurt you. (On the other hand, if you’re keeping 10% of the values, and the values are about the same size as the keys, you’ll actually save space.) That’s why it’s “almost always” a better solution, rather than “always”.