deepdiff icon indicating copy to clipboard operation
deepdiff copied to clipboard

Feature: Similarity instead of difference

Open AlexIsOnFire opened this issue 7 years ago • 6 comments

Would be great to be able to compare 2 objects for similar key values, for example: d1 = {"keyA": {"value": 10}, "keyB": {"value": 20}} d1 = {"keyA": {"value": 30}, "keyB": {"value": 20}}

diff = DeepDIff(d1, d2, reverse_diff=True) >> {"simillar_values": ["[keyB][value]"]}

I'm working on a tool that needs to compare Python dictionaries and not only find diffs (which this library does perfect!) , but also detect overlapping values.

AlexIsOnFire avatar Nov 22 '17 01:11 AlexIsOnFire

Once we properly implement the delta operator for DeepDiff then you can get the delta of a DeepDiff result from the original object which should give you what is the same between the 2.

seperman avatar Dec 16 '17 00:12 seperman

🧟

Since it seems that Delta is now implemented here in the future, and this issue is still open:

I'm trying to understand how to get similarity out of Delta, and I'm just not wrapping my head around it.

Any tips on how one might accomplish getting "unchanged values" out of a diff between two dictionaries?

Partial solution, but doesn't cover deeply nested values well: https://github.com/seperman/deepdiff/issues/203

lhriley avatar Nov 17 '23 20:11 lhriley

@lhriley You are correct. What I wrote in 2017 doesn't make sense. The delta object is only holding the difference between the 2 objects.

seperman avatar Nov 19 '23 15:11 seperman

I need to think about how this makes sense. For example, if in a nested dictionary, one value is changed, what do we report as similar?

t1 = {"key": {"a": {"key2": "val2", "key3": "val3", "key4": "val4"}}}
t1 = {"key": {"a": {"key2": "val2", "key3": "DIFFERENT!!", "key4": "val4"}}}

Do you expected something like this?

root["key"]["a"]["key2"]
root["key"]["a"]["key4"]

seperman avatar Nov 20 '23 21:11 seperman

@seperman that's a good question. I guess that I would expect a dict like object that goes as deep as the detected change.

Given:

t1 = {"key": {"a": {"key2": "val2", "key3": "val3", "key4": "val4"}}}
t2 = {"key": {"a": {"key2": "val2", "key3": "DIFFERENT!!", "key4": "val4"}}}

I could call:

delta.unchanged => {"key": {"a": {"key2": "val2", "key4": "val4"}}}
delta.changed => {"key": {"a": {"key3": "DIFFERENT!!"}}}

Then in my code I could do something like:

if delta.changed:
   print("values changed!")
   print(delta.changed)
   print(delta.diff)
   # do something about the change

if delta.unchanged:
   print("values didn't change. skipping.")
   print(delta.unchanged)
  # nothing to do for unchanged values

lhriley avatar Nov 21 '23 21:11 lhriley

Hi,

I would definitely use the feature as well. My scenario is two objects with some overlap, where I would like to make the second object a "pure delta" - without any elements already present in the first one.

I would expect a result to look identically to a diff, just like with comparison done with equal operator instead of non-equal.

lukjak avatar Mar 21 '24 20:03 lukjak