fork download
  1. sep = "---> "
  2.  
  3. input = """
  4. NP---> N_NNP
  5. NP---> N_NN_S_NU
  6. NP---> N_NNP
  7. NP---> N_NNP
  8. VGF---> V_VM_VF
  9. NP---> N_NN
  10. """
  11.  
  12. formatted = [x.strip().split(sep) for x in input.split("\n") if len(x.strip()) > 0]
  13.  
  14. output = {}
  15. prev_index,prev_key = 0,None
  16. for index,items in enumerate(formatted):
  17. prev_index = index if prev_key != items[0] else prev_index
  18. prev_key = items[0]
  19. output.setdefault("{0}_{1}".format(prev_key,prev_index),[]).append(items[1])
  20.  
  21. print("output is......")
  22. for key,result in output.iteritems():
  23. print("{0}{1}{2}".format(key.split("_")[0],sep," ".join(result)))
Success #stdin #stdout 0.01s 7852KB
stdin
Standard input is empty
stdout
output is......
NP--->  N_NN
VGF--->  V_VM_VF
NP--->  N_NNP N_NN_S_NU N_NNP N_NNP