Reputation: 483
I have some strings like 'english100'
and 'math50'
.
How can I convert them to a dictionary such as:
{'english': 100, 'math': 50}.
I have tried:
re.split(r'(?=\d)'
However, that does not work.
Upvotes: 0
Views: 153
Reputation: 309841
If your strings are that simple, I'd probably do something like this:
d = dict()
d.update(re.findall(r'([a-zA-Z]+)(\d+)',"english100"))
Or another way (if you have multiple occurrences in the same string):
>>> dict(x.groups() for x in re.finditer(r'([a-zA-Z]+)(\d+)',"english100spanish24"))
{'spanish': '24', 'english': '100'}
Upvotes: 3