for example, I get a string like "twenty two plus three", then I split the string into a list ["twenty two", "plus", "three"] and I want to get the first and last elements of the list as a number. How?
Asked
Active
Viewed 263 times
1
-
Does this answer your question? [How do I parse a string to a float or int?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int) – davetherock May 02 '21 at 21:16
-
Write a dictionary converting one-twenty to 1:20 (or use a list by index). Then write thirty, forty conversions to 30,40 etc. and figure out the logic for 31 etc. – Chris_Rands May 02 '21 at 21:19
-
2@davetherock this cannot answer his question, because it's about converting plain numbers with quotes around, but this question is about converting number words to numbers – TheEagle May 02 '21 at 21:21
-
Does this answer your question? [Is there a way to convert number words to Integers?](https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers) – buddemat May 02 '21 at 21:25
-
1Please go through the [intro tour](https://stackoverflow.com/tour), the [help center](https://stackoverflow.com/help) and [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to see how this site works and to help you improve your current and future questions, which can help you get better answers. "Show me how to solve this coding problem?" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a *specific* question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – Prune May 02 '21 at 21:25
-
1See [How much research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and the [Question Checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). – Prune May 02 '21 at 21:26
2 Answers
1
You can use a library word2number Link: https://pypi.org/project/word2number/
After installing it,
from word2number import w2n
print(w2n.word_to_num('twenty two'))
Output:
22

KnowledgeGainer
- 1,017
- 1
- 5
- 14
0
You can make a dict like this : dico_units = {'one':1, 'two':2 ...} dico_tens = {'ten':10, ...} caution to 11, 12, 13 then check wich number fits :)

Tom Kuntz
- 55
- 1
- 7