Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一些字符串,比如'english100'and 'math50'。如何将它们转换为字典,例如:
'english100'
'math50'
{'english': 100, 'math': 50}.
我试过了:
re.split(r'(?=\d)'
但是,这是行不通的。
如果你的字符串那么简单,我可能会做这样的事情:
d = dict() d.update(re.findall(r'([a-zA-Z]+)(\d+)',"english100"))
或另一种方式(如果您在同一字符串中多次出现):
>>> dict(x.groups() for x in re.finditer(r'([a-zA-Z]+)(\d+)',"english100spanish24")) {'spanish': '24', 'english': '100'}