fork(1) download
  1. def common_count(t0, t1):
  2. "returns the length of the longest common prefix"
  3. for i, pair in enumerate(zip(t0, t1)):
  4. if pair[0] != pair[1]:
  5. return i
  6. return i
  7.  
  8. def group_by_longest_prefix(iterable):
  9. "given a sorted list of strings, group by longest common prefix"
  10. longest = 0
  11. out = []
  12.  
  13. for t in iterable:
  14. if out: # for there are previous entries
  15.  
  16. # determine length of prefix in common with prev line
  17. common = common_count(t, out[-1])
  18.  
  19. # if the current entry has a shorted prefix, output the previous
  20. # entries then start a new group
  21. if common < longest:
  22. yield out
  23. longest = 0
  24. out = []
  25. # otherwise, just update the target prefix length
  26. else:
  27. longest = common
  28.  
  29. # add the current entry to the group
  30. out.append(t)
  31.  
  32. # ouput remaining entries as the last group
  33. if out:
  34. yield out
  35.  
  36. text = """
  37. TOKYO-BLING.1 H02-AVAILABLE
  38. TOKYO-BLING.1 H02-MIDDLING
  39. TOKYO-BLING.1 H02-TOP
  40. TOKYO-BLING.2 H04-USED
  41. TOKYO-BLING.2 H04-AVAILABLE
  42. TOKYO-BLING.2 H04-CANCELLED
  43. WAY-VERING.1 H03-TOP
  44. WAY-VERING.2 H03-USED
  45. WAY-VERING.2 H03-AVAILABLE
  46. WAY-VERING.1 H03-CANCELLED
  47. """
  48.  
  49. T = sorted(t.strip() for t in text.split("\n") if t)
  50.  
  51. for L in group_by_longest_prefix(T):
  52. print L
  53.  
  54.  
Success #stdin #stdout 0.08s 10864KB
stdin
Standard input is empty
stdout
['TOKYO-BLING.1 H02-AVAILABLE', 'TOKYO-BLING.1 H02-MIDDLING', 'TOKYO-BLING.1 H02-TOP']
['TOKYO-BLING.2 H04-AVAILABLE', 'TOKYO-BLING.2 H04-CANCELLED', 'TOKYO-BLING.2 H04-USED']
['WAY-VERING.1 H03-CANCELLED', 'WAY-VERING.1 H03-TOP']
['WAY-VERING.2 H03-AVAILABLE', 'WAY-VERING.2 H03-USED']