Sort List By Group


/ Published in: Python
Save to your folder(s)

This function takes a list of string and sorts them based on their similarity. The percent at which they match can be defined in the second parameter (default 75%).


Copy this code and paste it in your HTML
  1. def sortByGroup(lst, percent = 75):
  2. groups = []
  3. for item in lst:
  4. match = False
  5.  
  6. for g in xrange(len(groups)):
  7. group = groups[g]
  8. parent = group[0]
  9. points = 0.0
  10.  
  11. try:
  12. for x in xrange(len(parent)):
  13. if parent[x] == item[x]:
  14. points += 1
  15.  
  16. if (points / len(parent)) * 100 >= percent:
  17. group.append(item)
  18. group.sort()
  19. match = True
  20. except:
  21. pass
  22.  
  23. if not match:
  24. groups.append([item])
  25.  
  26. return groups
  27.  
  28. # Example:
  29. random = [
  30. 'bob1',
  31. 'frank2',
  32. 'bob3',
  33. 'joe2',
  34. 'frank1',
  35. 'bob2',
  36. 'joe1',
  37. 'joe3'
  38. ]
  39. groups = sortByGroup(random)
  40. for g in groups:
  41. for i in g:
  42. print i
  43. print '-' * 30
  44.  
  45. # Example Output:
  46. bob1
  47. bob2
  48. bob3
  49. ------------------------------
  50. frank1
  51. frank2
  52. ------------------------------
  53. joe1
  54. joe2
  55. joe3
  56. ------------------------------

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.