分割一个由字母和指数组成的python字符串

I have a string containing the literal part of a monomial. I should try to divide the letters from their exponents by inserting them into lists and assign exponent 1 if a letter does not have it.
strings are like that a^5bc^0.5

str = a^5bc^0.5
letters, exponents = split_monomial(str)
print(letters) # ['a','b','c']
print(exponents) # [5,1,0.5]

这是我尝试过的方法,但我认为存在概念错误

_ = True
numeric_cache = ''
        for i in range(len(litteral_part)):
            if litteral_part[i] in letters:
                if i > 0:
                    if not _:
                        litteral_e.append(float(numeric_cache))
                    litteral_e.append(1.0)
                _ = True
                litteral.append(litteral_part[i])
            elif litteral_part[i] == '^':
                _ = False
            elif litteral_part[i] in numbers:
                if not _:
                    numeric_cache = numeric_cache + litteral_part[i]

litteral_part是str

评论