Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- from operator import itemgetter
- def collect(seq, predicate):
- d = defaultdict(list)
- for item in seq:
- d[predicate(item)].append(item)
- return d
- splitted_filenames = [
- ("foo", "123", "txt", "foo_123.txt"),
- ("foo", "456", "dat", "foo_456.dat"),
- ("bar", "789", "py", "bar_789.py")
- ]
- filenames_grouped_by_name = collect(splitted_filenames, itemgetter(0))
- for category, filenames in filenames_grouped_by_name.items():
- if len(filenames) > 1:
- print("Found duplicates for filename", category)
- print("Renaming advised for these files:")
- for filename in filenames:
- print(filename[3])
- else:
- print("filename", category, "has no duplicates. No action required.")
- #result:
- # Found duplicates for filename foo
- # Renaming advised for these files:
- # foo_123.txt
- # foo_456.dat
- # filename bar has no duplicates. No action required.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement