如何从python中的字符串中返回word格式的数字



EDIT:"已经回答"并不是在说我是什么。我的字符串已经以word格式出现了。我需要将这些单词从字符串中剥离到一个列表中

我正试图在python中为语音助手处理短语操作。

当我说这样的话时:

"What is 15,276 divided by 5?"

它的格式如下:

"what is fifteen thousand two hundred seventy six divided by five"

我已经有办法将数学部分的字符串改为int,那么有没有办法从短语中获得这样的列表?

['fifteen thousand two hundred seventy six','five']

浏览单词列表,并检查每个单词在一组数字单词中的成员资格。将相邻单词添加到临时列表中。当你找到一个非数字单词时,创建一个新的列表。记住在句子开头解释非数字单词,在句子结尾解释数字单词。

result = []
group = []
nums = set('one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty thirty forty fifty sixty seventy eighty ninety hundred thousand million billion trillion quadrillion quintillion'.split())
for word in "what is fifteen thousand two hundred seventy six divided by five".split():
if word in nums:
group.append(word)
else:
if group:
result.append(group)
group = []
if group:
result.append(group)

结果:

>>> result
[['fifteen', 'thousand', 'two', 'hundred', 'seventy', 'six'], ['five']]

将每个子列表合并为一个字符串:

>>> list(map(' '.join, result))
['fifteen thousand two hundred seventy six', 'five']