Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. from collections import defaultdict
  2. from operator import itemgetter
  3. def collect(seq, predicate):
  4. d = defaultdict(list)
  5. for item in seq:
  6. d[predicate(item)].append(item)
  7. return d
  8.  
  9. splitted_filenames = [
  10. ("foo", "123", "txt", "foo_123.txt"),
  11. ("foo", "456", "dat", "foo_456.dat"),
  12. ("bar", "789", "py", "bar_789.py")
  13. ]
  14.  
  15. filenames_grouped_by_name = collect(splitted_filenames, itemgetter(0))
  16. for category, filenames in filenames_grouped_by_name.items():
  17. if len(filenames) > 1:
  18. print("Found duplicates for filename", category)
  19. print("Renaming advised for these files:")
  20. for filename in filenames:
  21. print(filename[3])
  22. else:
  23. print("filename", category, "has no duplicates. No action required.")
  24.  
  25. #result:
  26. # Found duplicates for filename foo
  27. # Renaming advised for these files:
  28. # foo_123.txt
  29. # foo_456.dat
  30. # filename bar has no duplicates. No action required.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement