• <i id='WbhYl'><tr id='WbhYl'><dt id='WbhYl'><q id='WbhYl'><span id='WbhYl'><b id='WbhYl'><form id='WbhYl'><ins id='WbhYl'></ins><ul id='WbhYl'></ul><sub id='WbhYl'></sub></form><legend id='WbhYl'></legend><bdo id='WbhYl'><pre id='WbhYl'><center id='WbhYl'></center></pre></bdo></b><th id='WbhYl'></th></span></q></dt></tr></i><div id='WbhYl'><tfoot id='WbhYl'></tfoot><dl id='WbhYl'><fieldset id='WbhYl'></fieldset></dl></div>
    <tfoot id='WbhYl'></tfoot>

    <small id='WbhYl'></small><noframes id='WbhYl'>

          <bdo id='WbhYl'></bdo><ul id='WbhYl'></ul>
      1. <legend id='WbhYl'><style id='WbhYl'><dir id='WbhYl'><q id='WbhYl'></q></dir></style></legend>

        有没有办法将数字单词转换为整数?

        时间:2023-09-11

              • <bdo id='oARzf'></bdo><ul id='oARzf'></ul>

                <tfoot id='oARzf'></tfoot>
              • <i id='oARzf'><tr id='oARzf'><dt id='oARzf'><q id='oARzf'><span id='oARzf'><b id='oARzf'><form id='oARzf'><ins id='oARzf'></ins><ul id='oARzf'></ul><sub id='oARzf'></sub></form><legend id='oARzf'></legend><bdo id='oARzf'><pre id='oARzf'><center id='oARzf'></center></pre></bdo></b><th id='oARzf'></th></span></q></dt></tr></i><div id='oARzf'><tfoot id='oARzf'></tfoot><dl id='oARzf'><fieldset id='oARzf'></fieldset></dl></div>

                <small id='oARzf'></small><noframes id='oARzf'>

                  <tbody id='oARzf'></tbody>
                <legend id='oARzf'><style id='oARzf'><dir id='oARzf'><q id='oARzf'></q></dir></style></legend>
                  本文介绍了有没有办法将数字单词转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要将one转换成1two转换成2等等.

                  I need to convert one into 1, two into 2 and so on.

                  有没有办法通过库或类或任何东西来做到这一点?

                  Is there a way to do this with a library or a class or anything?

                  推荐答案

                  这段代码大部分是设置numwords dict,只在第一次调用时完成.

                  The majority of this code is to set up the numwords dict, which is only done on the first call.

                  def text2int(textnum, numwords={}):
                      if not numwords:
                        units = [
                          "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
                          "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
                          "sixteen", "seventeen", "eighteen", "nineteen",
                        ]
                  
                        tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
                  
                        scales = ["hundred", "thousand", "million", "billion", "trillion"]
                  
                        numwords["and"] = (1, 0)
                        for idx, word in enumerate(units):    numwords[word] = (1, idx)
                        for idx, word in enumerate(tens):     numwords[word] = (1, idx * 10)
                        for idx, word in enumerate(scales):   numwords[word] = (10 ** (idx * 3 or 2), 0)
                  
                      current = result = 0
                      for word in textnum.split():
                          if word not in numwords:
                            raise Exception("Illegal word: " + word)
                  
                          scale, increment = numwords[word]
                          current = current * scale + increment
                          if scale > 100:
                              result += current
                              current = 0
                  
                      return result + current
                  
                  print text2int("seven billion one hundred million thirty one thousand three hundred thirty seven")
                  #7100031337
                  

                  这篇关于有没有办法将数字单词转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:显示带前导零的数字 下一篇:Python:TypeError:无法连接“str"和“int"对象

                  相关文章

                  最新文章

                    <bdo id='dSDnW'></bdo><ul id='dSDnW'></ul>

                  1. <legend id='dSDnW'><style id='dSDnW'><dir id='dSDnW'><q id='dSDnW'></q></dir></style></legend>

                    <small id='dSDnW'></small><noframes id='dSDnW'>

                    <i id='dSDnW'><tr id='dSDnW'><dt id='dSDnW'><q id='dSDnW'><span id='dSDnW'><b id='dSDnW'><form id='dSDnW'><ins id='dSDnW'></ins><ul id='dSDnW'></ul><sub id='dSDnW'></sub></form><legend id='dSDnW'></legend><bdo id='dSDnW'><pre id='dSDnW'><center id='dSDnW'></center></pre></bdo></b><th id='dSDnW'></th></span></q></dt></tr></i><div id='dSDnW'><tfoot id='dSDnW'></tfoot><dl id='dSDnW'><fieldset id='dSDnW'></fieldset></dl></div>

                      <tfoot id='dSDnW'></tfoot>