Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- def prompt_list(prompt):
- print(prompt)
- print("(Enter each list item on a separate line. Press Ctrl+D or enter a blank line to finish.)")
- items = []
- try:
- while True:
- inp = input().strip()
- if inp == "": return items
- items.append(inp)
- except EOFError:
- return items
- def tag(t, lst):
- return [ (x, t) for x in lst ]
- lists = [ f"Shopping list {i + 1}" for i in range(4) ]
- items = []
- for list_name in lists:
- items.extend(tag(list_name, prompt_list(list_name + ": ")))
- item_counts = {}
- for item, _ in items:
- item_counts[item] = item_counts.get(item, 0) + 1
- # find all items appearing in only 1 list
- unique_items = [ (item, tag) for item, tag in items if item_counts[item] == 1 ]
- for list_name in lists:
- print(f"{list_name}'s unique items:")
- print("\n".join([ item for item, tag in unique_items if tag == list_name ]))
- print("Top items:")
- for top_item, count in sorted(item_counts.items(), key=lambda item_pair: item_pair[1], reverse=True)[:3]:
- print(f"{top_item}: {count}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement