Advertisement
osmarks

Untitled

Feb 22nd, 2021
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. def prompt_list(prompt):
  4.     print(prompt)
  5.     print("(Enter each list item on a separate line. Press Ctrl+D or enter a blank line to finish.)")
  6.     items = []
  7.     try:
  8.         while True:
  9.             inp = input().strip()
  10.             if inp == "": return items
  11.             items.append(inp)
  12.     except EOFError:
  13.         return items
  14.  
  15. def tag(t, lst):
  16.     return [ (x, t) for x in lst ]
  17.  
  18. lists = [ f"Shopping list {i + 1}" for i in range(4) ]
  19. items = []
  20. for list_name in lists:
  21.     items.extend(tag(list_name, prompt_list(list_name + ": ")))
  22.  
  23. item_counts = {}
  24. for item, _ in items:
  25.     item_counts[item] = item_counts.get(item, 0) + 1
  26.  
  27. # find all items appearing in only 1 list
  28. unique_items = [ (item, tag) for item, tag in items if item_counts[item] == 1 ]
  29. for list_name in lists:
  30.     print(f"{list_name}'s unique items:")
  31.     print("\n".join([ item for item, tag in unique_items if tag == list_name ]))
  32.  
  33. print("Top items:")
  34. for top_item, count in sorted(item_counts.items(), key=lambda item_pair: item_pair[1], reverse=True)[:3]:
  35.     print(f"{top_item}: {count}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement