Arijit Arijit - 3 years ago 81
Python Question

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

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.

Answer Source

You're on the right track, but you should use a slightly different regular expression and use re.findall. Like this:

In [1]: string1 = "47482M4I14M7I7M1I26M8D25M4I20M2I11M7I17M7I7M22I14M3I35M3I30M1D15M2I16M17D4M5D15M7D37M1D24M5D5M6D27M4I35M11I10M3I5M3I24M15I175M3D13M236792H"

In [2]: import re, collections

In [3]: p = re.compile("([0-9]+)([A-Za-z])")

In [4]: dct = collections.defaultdict(list)

In [5]: for number, letter in p.findall(string1):
    ...:     dct[letter].append(number)
    ...:      

In [6]: dct
Out[6]: 
defaultdict(list,
            {'D': ['8', '1', '17', '5', '7', '1', '5', '6', '3'],
             'H': ['236792'],
             'I': ['4', '7', '1', '4', '2', '7', '7', '22', '3', '3', '2', '4', '11', '3', '3', '15'],
             'M': ['47482', '14', '7', '26', '25', '20', '11', '17', '7', '14', '35', '30', '15', '16', '4', '15', '37', '24', '5', '27', '35', '10', '5', '24', '175', '13']})

This locates all pairs of numbers followed by a letter in the string and puts all those pairs into a dictionary with the letter as key, duplicate numbers are allowed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download