fork download
  1. import re
  2.  
  3. strings = [
  4. 'abcd1-2 4d4e',
  5. 'xyz0-1 551',
  6. 'foo 3ea',
  7. 'bar1 2bd',
  8. 'mc-mqisd0-2 77a'
  9. ]
  10.  
  11. def listFln(listA):
  12. dct = {}
  13. for s in listA:
  14. lst = sum(re.findall(r"^(\S*?)(?:(\d+)-(\d+))?\s+(.*)", s), ())
  15. if lst and lst[2]:
  16. for i in range(int(lst[1]), int(lst[2]) + 1):
  17. dct[lst[0] + str(i)] = lst[3]
  18. else:
  19. dct[lst[0]] = lst[3]
  20. return dct
  21.  
  22.  
  23. print(listFln(strings))
Success #stdin #stdout 0.03s 9832KB
stdin
Standard input is empty
stdout
{'abcd1': '4d4e', 'abcd2': '4d4e', 'xyz0': '551', 'xyz1': '551', 'foo': '3ea', 'bar1': '2bd', 'mc-mqisd0': '77a', 'mc-mqisd1': '77a', 'mc-mqisd2': '77a'}