how to split a string and return its separator in python?

Viewed 115

I have a string that looks like below:

string1 = "47482M4I14M7I7M1I26M8D25M4I20M2I11M7I17M7I7M22I14M3I35M3I30M1D15M2I16M17D4M5D15M7D37M1D24M5D5M6D27M4I35M11I10M3I5M3I24M15I175M3D13M236792H"

I want to separate by alphabets(i.e. A-Z or a-z) and put the associated value with in a dictionary of lists. Each set of number is associated with alphabets. for example,

'M' is associated with 47482, 14, 7I7 etc.

'I' is associated with 4, 1 etc

'H' is associated with 236792

My final data structure will be like

    dict = { 
      M:[47482, 14, 717],
      I:[4, 1],
      H:[236792]

    }

My try:

import re
string1 = "47482M4I14M7I7M1I26M8D25M4I20M2I11M7I17M7I7M22I14M3I35M3I30M1D15M2I16M17D4M5D15M7D37M1D24M5D5M6D27M4I35M11I10M3I5M3I24M15I175M3D13M236792H"
tmp = re.split('[a-zA-Z]', string1)
print(tmp)

I am unable to get those alphabets as separators. Need help to create the data structure.

5 Answers
Related