将单词"single"转换为数字 1 的 Python 包



我正在寻找一种方法来转换单词"单个"双";,等等转换成数字。到目前为止,我能找到的只有word2number和text2digits,但它们都做不到。有人知道能做到这一点的软件包吗?

为了明智地解决这个问题,@Barmar找到了答案-

你可以创建一本字典,然后简单地对照来测试你的单词

tuple_word_conversions = {
"single": 1,
"double": 2,
"couple": 2,
"triple": 3,
...
}
>>> test_words
['double', 'scoop', 'ice', 'cream']
>>> for index, word in enumerate(test_words.copy()):
...     if (value := tuple_word_conversions.get(word)) is not None:
...         test_words[index] = value  # NOTE int without other changes
...
>>> test_words
[2, 'scoop', 'ice', 'cream']

最新更新