math_str = "one times one plus sin(one hundred fifty three) minus three billion"
getNumberWords(math_str) #Returns one, one, one hundred fifty three, three billion
math_str = "one times one plus sin(one hundred fifty three) minus three billion"
allowed = ['one', 'three', 'fifty', 'hundred', 'thousand', 'million', 'billion']
def getNumberWords(math_str):
math_str = math_str.replace('(', ' ')
math_str = math_str.replace(')', ' ')
math_str = math_str.split()
return [word for word in math_str if word in allowed]
print(getNumberWords(math_str))
这没有捷径,因为python不懂英语或人类语言,您需要有一个被视为数字单词的单词列表
在本例中,我只是输入了获得结果所需的字数,但如果希望结果准确,则需要填写大量的字数(数字)
相关问题 更多 >
编程相关推荐