Created
May 19, 2019 07:18
-
-
Save arsho/933c12171f6e0c6d05622f83f671b74d to your computer and use it in GitHub Desktop.
Shanto mapping debug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge_two_lists(a, b): | |
data = {} | |
for pair in a+b: | |
key, value = pair | |
data[key] = data.get(key, 0) + value | |
sorted_data = sorted([[key, value] for key, value in data.items()]) | |
return sorted_data | |
if __name__ == '__main__': | |
a = [['hello', 5], ['test', 6], ['egg', 2]] | |
b = [['cat', 5], ['dog', 6], ['hello', 3], ['test', 2]] | |
sorted_data = merge_two_lists(a, b) | |
print(sorted_data) | |
# Output: [['cat', 5], ['dog', 6], ['egg', 2], ['hello', 8], ['test', 8]] | |
# The following section is for testing | |
expected_result = sorted([['hello', 8], | |
['test', 8], | |
['cat', 5], | |
['dog', 6], | |
['egg', 2]]) | |
assert sorted_data == expected_result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment