Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Iterable
- list1 = ["toto", "titi", "tata", "titi"]
- list2 = ["tutu", "titi", "tata", "tutu"]
- def count_occurrences(input_list: Iterable) -> dict:
- result = {}
- for x in input_list:
- result[x] = result.get(x, 0) + 1
- return result
- def get_diff(old_list: Iterable, new_list: Iterable) -> Iterable:
- old_counts = count_occurrences(old_list)
- new_counts = count_occurrences(new_list)
- result = []
- for element, old_count in old_counts.items():
- new_count = new_counts.get(element, 0)
- diff_count = new_count - old_count
- if diff_count < 0:
- for _ in range(-diff_count):
- result.append("del, \"%s\"" % element)
- for element, new_count in new_counts.items():
- old_count = old_counts.get(element, 0)
- diff_count = new_count - old_count
- if diff_count > 0 :
- for _ in range(diff_count):
- result.append("add, \"%s\"" % element)
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement