There are probably many pitfalls to a generic approach like this, but if your
dictionaries are of mostly primitives, and not huge, you can do something like
this:
Assuming your data looks something like this:
networks = [ {'address': '192.168.1.1'}, {'address': '127.0.0.1'}, ]missing = [ {'address': '127.0.0.1'} ]
You can turn the lists of dictionaries into lists tuples (which are hashable)
def make_hashable(d): return (frozenset(x.iteritems()) for x in d)networks_hashable = make_hashable(networks)missing_hashable = make_hashable(missing)
Then subtract
diff = set(networks_hashable).difference(missing_hashable)
Now you have a list of tuples
print list(diff)
or, convert back to dictionaries
print [dict(x) for x in diff]
Update
I’ve changed the definition of
make_hashablebased on @gnibbler’s comment.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)