furas

Python - sorting by tails - (Stackoverflow)

Feb 27th, 2022 (edited)
238
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Stack Overflow: https://stackoverflow.com/questions/71285098/sort-strings-based-on-tail-component-of-word
  2.  
  3. from collections import Counter
  4.  
  5. text = "Headword,Headstand,Tailword,Middlelistword,Headlistword,Tailwording"
  6. words = text.split(',')
  7.  
  8. # start with shortest word
  9. size = len(min(words, key=len))
  10.  
  11. all_tuples = []
  12.  
  13. for s in range(size, 1, -1):
  14.     tails = [w[-s:] for w in words]
  15.     tails_counts = Counter(tails)
  16.  
  17.     common_tails = [key for key, val in tails_counts.items() if val > 1]
  18.     print('common:', common_tails)
  19.    
  20.     for c in common_tails:
  21.         temp_words = []
  22.         for w in words:
  23.              if w.endswith(c):
  24.                 # remove to tuples
  25.                 all_tuples.append( (c, w) )
  26.              else:
  27.                 # keep it
  28.                 temp_words.append(w)
  29.         words = temp_words
  30.          
  31.     print('words: ', words)
  32.     print('---')
  33.  
  34. # add other words with tail which will be sorted to the end of list
  35. for w in words:
  36.     all_tuples.append( (chr(65535), w) )
  37.  
  38. print(all_tuples)
  39. print('---')
  40.  
  41. # sorting and displaying
  42. for item in sorted(all_tuples):
  43.     print(item[1])
RAW Paste Data Copied