-
Notifications
You must be signed in to change notification settings - Fork 28
IMPROVE merge_2_dicts #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,23 +89,33 @@ def merge_2_dicts(dict1, dict2, value_merge_func=None): | |
| if dict1 is None: | ||
| return dict2 | ||
|
|
||
| def merged_value(key): | ||
| if key not in dict1: | ||
| return dict2[key] | ||
| elif key not in dict2: | ||
| return dict1[key] | ||
| else: | ||
| if value_merge_func is None: | ||
| raise ValueError( | ||
| "Conflict in merged dictionaries: merge function not " | ||
| "provided but key {} exists in both dictionaries".format( | ||
| key)) | ||
|
|
||
| return value_merge_func(dict1[key], dict2[key]) | ||
|
|
||
| keys = set(dict1.keys()) | set(dict2.keys()) | ||
|
|
||
| return {key: merged_value(key) for key in keys} | ||
| if dict1 == dict2: | ||
| for k, v in dict1.items(): | ||
| dict1[k] = value_merge_func(v, v) | ||
| return dict1 | ||
|
|
||
| dict1_set = set(dict1) | ||
| dict2_set = set(dict2) | ||
|
|
||
| keys_to_merge = dict1_set.intersection(dict2_set) | ||
|
|
||
| if len(keys_to_merge) == 0 and value_merge_func is None: | ||
|
||
| raise ValueError( | ||
| "Conflict in merged dictionaries: merge function not " | ||
| "provided but keys {} exists in both dictionaries".format( | ||
| keys_to_merge)) | ||
|
|
||
| values_merged = dict() | ||
|
|
||
| for key_to_merge in keys_to_merge: | ||
| old_value1 = dict1[key_to_merge] | ||
| old_value2 = dict2[key_to_merge] | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unless really proven to be necessary for optimisations reasons, I would discourage mutating I think that thanks to the order you put in We obtain the equivalent logic. What do you think ? |
||
| new_value = value_merge_func(old_value1, old_value2) | ||
|
|
||
| values_merged[key_to_merge] = new_value | ||
|
|
||
| return {**dict1, **dict2, **values_merged} | ||
|
|
||
|
|
||
| def df_concat(d1, d2): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to call
value_merge_func()? why not just returningdict1? I just did a quick check, python's==seems to honor__eq__()on the dictionnary values => if==returns true, that means those dicts are equal and I think do not need to be mergedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the merge function is for example
add 1, you still need to apply the merge function.