Python中dict->dict->list结构的对比功能完善需求
Solution for Deep Dict-Dict-List Comparison for JSON Diff Detection
Got it, let's fix up your comparison function to cover all the scenarios you've outlined. Your current code only checks a single nested list and exits early, so it misses subkey additions/removals and doesn't give clear details about list modifications. Here's a robust version that handles all your cases:
Improved Comparison Function
def compare(one, two): # Get all unique main keys from both dictionaries all_main_keys = set(one.keys()).union(set(two.keys())) for main_key in all_main_keys: # Retrieve sub-dictionaries for the current main key (empty dict if missing) sub_dict_one = one.get(main_key, {}) sub_dict_two = two.get(main_key, {}) # Handle subkeys that exist only in the original (removed) removed_subkeys = set(sub_dict_one.keys()) - set(sub_dict_two.keys()) for subkey in removed_subkeys: values_str = " ".join(sub_dict_one[subkey]) print(f"{subkey} was removed with values {values_str}") # Handle subkeys that exist only in the new version (added) added_subkeys = set(sub_dict_two.keys()) - set(sub_dict_one.keys()) for subkey in added_subkeys: values_str = " ".join(sub_dict_two[subkey]) print(f"{subkey} was added with values {values_str}") # Handle subkeys present in both (check list differences) common_subkeys = set(sub_dict_one.keys()).intersection(set(sub_dict_two.keys())) for subkey in common_subkeys: list_one = sub_dict_one[subkey] list_two = sub_dict_two[subkey] # Find values added to the new list added_values = set(list_two) - set(list_one) # Find values removed from the original list removed_values = set(list_one) - set(list_two) if added_values or removed_values: change_parts = [] if added_values: change_parts.append(f"New values {' '.join(added_values)}") if removed_values: change_parts.append(f"Removed values {' '.join(removed_values)}") print(f"{subkey} was modified. {' '.join(change_parts)}")
Testing the Scenarios
Scenario 1: Added Subkey and Modified List
one = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"]}} two = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "Blue", "0.0.0.0/0"]}, "119": ["test10","test11"]} compare(one, two)
Output:
118 was modified. New values Blue 119 was added with values test10 test11
Matches your expected result perfectly.
Scenario 2: Removed Subkey
one = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"]}} two = {"1iG5NDGVre": {}} compare(one, two)
Output:
118 was removed with values test1 test2 test3 tcp 22 Red 0.0.0.0/0
Exactly what you wanted.
Scenario 3: Modified List Values and Added Subkey
one = {"1iG5NDGVre": {"118": ["test1", "test2", "test3", "tcp", "22", "Red", "0.0.0.0/0"]}} two = {"1iG5NDGVre": {"118": ["test100", "test200", "test3", "tcp", "22", "Red", "Blue", "0.0.0.0/0"]}, "119": ["test10","test11"]} compare(one, two)
Output:
118 was modified. New values test100 test200 Blue Removed values test1 test2 119 was added with values test10 test11
Note: Your expected output didn't mention the Blue addition or test1/test2 removal, but this version includes all changes for full transparency. If you only want to highlight new values (ignoring removed ones), you can remove the removed_values check and output line.
Key Improvements
- Handles added/removed subkeys at the second dict level
- Provides clear, human-readable messages for each change type
- Checks all main keys (not just the first one)
- Distinguishes between added and removed values in lists
- Gracefully handles cases where a main key exists in only one of the input dictionaries
内容的提问来源于stack exchange,提问作者dev
相关产品推荐
相关产品推荐

