Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. from typing import Iterable
  2.  
  3. list1 = ["toto", "titi", "tata", "titi"]
  4. list2 = ["tutu", "titi", "tata", "tutu"]
  5.  
  6.  
  7. def count_occurrences(input_list: Iterable) -> dict:
  8.     result = {}
  9.     for x in input_list:
  10.         result[x] = result.get(x, 0) + 1
  11.     return result
  12.  
  13.  
  14. def get_diff(old_list: Iterable, new_list: Iterable) -> Iterable:
  15.     old_counts = count_occurrences(old_list)
  16.     new_counts = count_occurrences(new_list)
  17.  
  18.     result = []
  19.  
  20.     for element, old_count in old_counts.items():
  21.         new_count = new_counts.get(element, 0)
  22.         diff_count = new_count - old_count
  23.  
  24.         if diff_count < 0:
  25.             for _ in range(-diff_count):
  26.                 result.append("del, \"%s\"" % element)
  27.  
  28.     for element, new_count in new_counts.items():
  29.         old_count = old_counts.get(element, 0)
  30.         diff_count = new_count - old_count
  31.  
  32.         if diff_count > 0 :
  33.             for _ in range(diff_count):
  34.                 result.append("add, \"%s\"" % element)
  35.  
  36.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement