我需要将one转换为1,将two转换为2,依此类推。

是否可以使用库或类或其他任何方法进行此操作?

评论

另请参阅:stackoverflow.com/questions/70161/…

也许这会有所帮助:pastebin.com/WwFCjYtt

如果有人仍在寻找答案,我将从下面的所有答案中汲取灵感,并创建了一个python包:github.com/careless25/text2digits

我使用以下示例来开发和扩展此过程,但将其扩展为西班牙语,以供将来参考:github.com/elbaulp/text2digits_es

#1 楼

此代码的大部分是设置数字词dict,仅在首次调用时执行。

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


评论


仅供参考,这不适用于日期。尝试:打印text2int(“十九个九十六”)#115

–尼克·鲁伊斯(Nick Ruiz)
2014年5月13日14:26



用数字表达1996年的正确方法是“一千九百九十六”。如果要支持几年,则需要其他代码。

–递归
14年5月13日在15:08

马克·伯恩斯(Marc Burns)有一颗红宝石可以做到这一点。我最近叉了它以增加支持多年。您可以从python调用ruby代码。

– Dimid
15年5月5日在20:14

它尝试了“一百零六次”。 print(text2int(“ 106”)..同时print(text2int(“ thousand”))

– Harish Kayarohanam
17-2-26在8:43



写下这些数字的正确方法是106和1000。但是,如果您需要处理这些情况,请随时添加支持。

–递归
17-2-26在15:27

#2 楼

我刚刚为确切的目的向PyPI发布了一个名为word2number的python模块。 https://github.com/akshaynagpal/w2n

使用以下方法安装:

pip install word2number


确保您的点已更新至最新版本。

用法:

from word2number import w2n

print w2n.word_to_num("two million three thousand nine hundred and eighty four")
2003984


评论


试过你的包裹。建议处理类似“ 100万”或“ 1M”的字符串。 w2n.word_to_num(“ 1百万”)引发错误。

–雷
16年5月4日19:50

@Ray感谢您尝试一下。您能在github.com/akshaynagpal/w2n/issues上提出一个问题吗?如果愿意,您也可以做出贡献。否则,我一定会在下一版本中讨论此问题。再次感谢!

–akshaynagpal
16年5月4日在20:33

Robert,开放源代码软件是关于人们协同改进它的全部内容。我想要一个图书馆,看到人们也想要一个。做到了。它可能尚未准备好用于生产级别的系统,或未符合教科书的流行语。但是,它是为此目的而工作的。此外,如果您可以提交PR,以便为所有用户提供进一步的改善,那就太好了。

–akshaynagpal
16年8月7日在6:27

它会进行计算吗?说:百分之十七十七?或任何其他运算符,即+,6,*和/

– S.Jackson
11月5日9:28

截止到现在@ S.Jackson。

–akshaynagpal
11月6日,0:58



#3 楼

如果有人感兴趣,我会破解一个保留其余字符串的版本(尽管它可能有错误,但并没有对其进行过多测试)。

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)

    ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
    ordinal_endings = [('ieth', 'y'), ('th', '')]

    textnum = textnum.replace('-', ' ')

    current = result = 0
    curstring = ""
    onnumber = False
    for word in textnum.split():
        if word in ordinal_words:
            scale, increment = (1, ordinal_words[word])
            current = current * scale + increment
            if scale > 100:
                result += current
                current = 0
            onnumber = True
        else:
            for ending, replacement in ordinal_endings:
                if word.endswith(ending):
                    word = "%s%s" % (word[:-len(ending)], replacement)

            if word not in numwords:
                if onnumber:
                    curstring += repr(result + current) + " "
                curstring += word + " "
                result = current = 0
                onnumber = False
            else:
                scale, increment = numwords[word]

                current = current * scale + increment
                if scale > 100:
                    result += current
                    current = 0
                onnumber = True

    if onnumber:
        curstring += repr(result + current)

    return curstring


例如:

 >>> text2int("I want fifty five hot dogs for two hundred dollars.")
 I want 55 hot dogs for 200 dollars.


如果您说“ $ 200”,可能会出现问题。但是,这真的很粗糙。

评论


我从这里获取了此代码片段和其他代码片段,并将其放入python库:github.com/careless25/text2digits

– stackErr
19-3-31在2:52



#4 楼

我需要一些不同的东西,因为我的输入是从语音到文本的转换,解决方案并不总是对数字求和。例如,“我的邮政编码为一二三四五”不应转换为“我的邮政编码为15”。

我接受了安德鲁的回答,并对其进行了调整,以处理人们强调为错误的其他一些情况,并且还增加了对上面提到的邮政编码等示例的支持。下面显示了一些基本的测试用例,但我确定仍有改进的余地。

def is_number(x):
    if type(x) == str:
        x = x.replace(',', '')
    try:
        float(x)
    except:
        return False
    return True

def text2int (textnum, 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']
    ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
    ordinal_endings = [('ieth', 'y'), ('th', '')]

    if not numwords:
        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)

    textnum = textnum.replace('-', ' ')

    current = result = 0
    curstring = ''
    onnumber = False
    lastunit = False
    lastscale = False

    def is_numword(x):
        if is_number(x):
            return True
        if word in numwords:
            return True
        return False

    def from_numword(x):
        if is_number(x):
            scale = 0
            increment = int(x.replace(',', ''))
            return scale, increment
        return numwords[x]

    for word in textnum.split():
        if word in ordinal_words:
            scale, increment = (1, ordinal_words[word])
            current = current * scale + increment
            if scale > 100:
                result += current
                current = 0
            onnumber = True
            lastunit = False
            lastscale = False
        else:
            for ending, replacement in ordinal_endings:
                if word.endswith(ending):
                    word = "%s%s" % (word[:-len(ending)], replacement)

            if (not is_numword(word)) or (word == 'and' and not lastscale):
                if onnumber:
                    # Flush the current number we are building
                    curstring += repr(result + current) + " "
                curstring += word + " "
                result = current = 0
                onnumber = False
                lastunit = False
                lastscale = False
            else:
                scale, increment = from_numword(word)
                onnumber = True

                if lastunit and (word not in scales):                                                                                                                                                                                                                                         
                    # Assume this is part of a string of individual numbers to                                                                                                                                                                                                                
                    # be flushed, such as a zipcode "one two three four five"                                                                                                                                                                                                                 
                    curstring += repr(result + current)                                                                                                                                                                                                                                       
                    result = current = 0                                                                                                                                                                                                                                                      

                if scale > 1:                                                                                                                                                                                                                                                                 
                    current = max(1, current)                                                                                                                                                                                                                                                 

                current = current * scale + increment                                                                                                                                                                                                                                         
                if scale > 100:                                                                                                                                                                                                                                                               
                    result += current                                                                                                                                                                                                                                                         
                    current = 0                                                                                                                                                                                                                                                               

                lastscale = False                                                                                                                                                                                                              
                lastunit = False                                                                                                                                                
                if word in scales:                                                                                                                                                                                                             
                    lastscale = True                                                                                                                                                                                                         
                elif word in units:                                                                                                                                                                                                             
                    lastunit = True

    if onnumber:
        curstring += repr(result + current)

    return curstring


一些测试...

one two three -> 123
three forty five -> 345
three and forty five -> 3 and 45
three hundred and forty five -> 345
three hundred -> 300
twenty five hundred -> 2500
three thousand and six -> 3006
three thousand six -> 3006
nineteenth -> 19
twentieth -> 20
first -> 1
my zip is one two three four five -> my zip is 12345
nineteen ninety six -> 1996
fifty-seventh -> 57
one million -> 1000000
first hundred -> 100
I will buy the first thousand -> I will buy the 1000  # probably should leave ordinal in the string
thousand -> 1000
hundred and six -> 106
1 million -> 1000000


评论


我接受了您的回答,并修复了一些错误。增加了对“二十十”-> 2010和所有十数的支持。您可以在这里找到它:github.com/careless25/text2digits

– stackErr
19年3月31日在2:51

这似乎是最好的!谢谢@totalhack

–user3480922
1月2日7:03

它会进行计算吗?说:百分之十七十七?或任何其他运算符,即+,6,*和/

– S.Jackson
11月5日9:29

#5 楼

感谢您的代码片段……为我节省了很多时间!

我需要处理几个额外的解析案例,例如序数词(“第一个”,“第二个”),带连字符的词(“一百个”)和带连字符的序数词(例如“五十个”)。 -seventh“),因此我添加了几行:

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)

    ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 'eighth':8, 'ninth':9, 'twelfth':12}
    ordinal_endings = [('ieth', 'y'), ('th', '')]

    textnum = textnum.replace('-', ' ')

    current = result = 0
    for word in textnum.split():
        if word in ordinal_words:
            scale, increment = (1, ordinal_words[word])
        else:
            for ending, replacement in ordinal_endings:
                if word.endswith(ending):
                    word = "%s%s" % (word[:-len(ending)], replacement)

            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`


评论


注意:这将返回百分之一,千分之一等的零。使用百分位数得到100!

– rohithpr
16 Mar 26 '16 at 18:50

#6 楼

这是微不足道的案例方法:

>>> number = {'one':1,
...           'two':2,
...           'three':3,}
>>> 
>>> number['two']
2


还是您正在寻找可以处理“十二万一千七百二十二”的东西?

#7 楼

这是第一个答案中的代码的C#实现:

public static double ConvertTextToNumber(string text)
{
    string[] units = new string[] {
        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
        "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
        "sixteen", "seventeen", "eighteen", "nineteen",
    };

    string[] tens = new string[] {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

    string[] scales = new string[] { "hundred", "thousand", "million", "billion", "trillion" };

    Dictionary<string, ScaleIncrementPair> numWord = new Dictionary<string, ScaleIncrementPair>();
    numWord.Add("and", new ScaleIncrementPair(1, 0));
    for (int i = 0; i < units.Length; i++)
    {
        numWord.Add(units[i], new ScaleIncrementPair(1, i));
    }

    for (int i = 1; i < tens.Length; i++)
    {
        numWord.Add(tens[i], new ScaleIncrementPair(1, i * 10));                
    }

    for (int i = 0; i < scales.Length; i++)
    {
        if(i == 0)
            numWord.Add(scales[i], new ScaleIncrementPair(100, 0));
        else
            numWord.Add(scales[i], new ScaleIncrementPair(Math.Pow(10, (i*3)), 0));
    }

    double current = 0;
    double result = 0;

    foreach (var word in text.Split(new char[] { ' ', '-', '—'}))
    {
        ScaleIncrementPair scaleIncrement = numWord[word];
        current = current * scaleIncrement.scale + scaleIncrement.increment;
        if (scaleIncrement.scale > 100)
        {
            result += current;
            current = 0;
        }
    }
    return result + current;
}


public struct ScaleIncrementPair
{
    public double scale;
    public int increment;
    public ScaleIncrementPair(double s, int i)
    {
        scale = s;
        increment = i;
    }
}


评论


这就是我喜欢的-看到答案的扩展,它们以不同的方式扩展以实现相同的答案。因为问题已经得到回答,所以以查询者未指定的语言来实现它不会有任何问题。但这确实可以帮助那些尝试实施代码的人。为了帮助以后的读者了解此问题,+ 1

–user1881400
13年8月30日在4:32

#8 楼

如果您想解析的数字数量有限,则可以很容易地将其硬编码到字典中。

对于稍微复杂的情况,您可能希望基于相对简单的数字语法自动生成此字典。与此类似的东西(当然是概括性的...)

for i in range(10):
   myDict[30 + i] = "thirty-" + singleDigitsDict[i]


如果您需要更广泛的内容,则似乎需要自然语言处理工具。本文可能是一个很好的起点。

#9 楼

e_h的C#实现的快速而又肮脏的Java端口(如上所述)。请注意,它们都返回双精度值,而不是整数。

public class Text2Double {

    public double Text2Double(String text) {

        String[] units = new String[]{
                "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
                "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
                "sixteen", "seventeen", "eighteen", "nineteen",
        };

        String[] tens = new String[]{"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};

        String[] scales = new String[]{"hundred", "thousand", "million", "billion", "trillion"};

        Map<String, ScaleIncrementPair> numWord = new LinkedHashMap<>();
        numWord.put("and", new ScaleIncrementPair(1, 0));


        for (int i = 0; i < units.length; i++) {
            numWord.put(units[i], new ScaleIncrementPair(1, i));
        }

        for (int i = 1; i < tens.length; i++) {
            numWord.put(tens[i], new ScaleIncrementPair(1, i * 10));
        }

        for (int i = 0; i < scales.length; i++) {
            if (i == 0)
                numWord.put(scales[i], new ScaleIncrementPair(100, 0));
            else
                numWord.put(scales[i], new ScaleIncrementPair(Math.pow(10, (i * 3)), 0));
        }

        double current = 0;
        double result = 0;

        for(String word : text.split("[ -]"))
        {
            ScaleIncrementPair scaleIncrement = numWord.get(word);
            current = current * scaleIncrement.scale + scaleIncrement.increment;
            if (scaleIncrement.scale > 100) {
                result += current;
                current = 0;
            }
        }
        return result + current;
    }
}

public class ScaleIncrementPair
{
    public double scale;
    public int increment;

    public ScaleIncrementPair(double s, int i)
    {
        scale = s;
        increment = i;
    }
}


#10 楼

进行更改,以便text2int(scale)将返回正确的转换。例如,text2int(“ hundred”)=>100。

import re

numwords = {}


def text2int(textnum):

    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", 
                'quadrillion', 'quintillion', 'sexillion', 'septillion', 
                'octillion', 'nonillion', 'decillion' ]

        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)

    ordinal_words = {'first':1, 'second':2, 'third':3, 'fifth':5, 
            'eighth':8, 'ninth':9, 'twelfth':12}
    ordinal_endings = [('ieth', 'y'), ('th', '')]
    current = result = 0
    tokens = re.split(r"[\s-]+", textnum)
    for word in tokens:
        if word in ordinal_words:
            scale, increment = (1, ordinal_words[word])
        else:
            for ending, replacement in ordinal_endings:
                if word.endswith(ending):
                    word = "%s%s" % (word[:-len(ending)], replacement)

            if word not in numwords:
                raise Exception("Illegal word: " + word)

            scale, increment = numwords[word]

        if scale > 1:
            current = max(1, current)

        current = current * scale + increment
        if scale > 100:
            result += current
            current = 0

    return result + current


评论


我认为正确的英文拼写100是“一百”。

–递归
2011年4月27日在20:14

@recursive绝对正确,但是此代码的优点是它可以处理“一百”(也许这就是Dawa试图强调的内容)。从描述的声音来看,其他类似的代码需要“百分之一百”,而这并不总是常用的术语(例如,如“她挑出百分之一百要丢弃的物品”)

–尼尔
16-12-29在23:05

#11 楼

马克·伯恩斯(Marc Burns)有一颗红宝石可以做到这一点。我最近叉了它以增加支持多年。您可以从python调用ruby代码。

  require 'numbers_in_words'
  require 'numbers_in_words/duck_punch'

  nums = ["fifteen sixteen", "eighty five sixteen",  "nineteen ninety six",
          "one hundred and seventy nine", "thirteen hundred", "nine thousand two hundred and ninety seven"]
  nums.each {|n| p n; p n.in_numbers}


结果:"fifteen sixteen" 1516 "eighty five sixteen" 8516 "nineteen ninety six" 1996 "one hundred and seventy nine" 179 "thirteen hundred" 1300 "nine thousand two hundred and ninety seven" 9297

评论


请不要调用来自python的ruby代码或来自ruby的python代码。它们之间的距离足够近,这样的东西应该可以移植过来。

– yekta
16-10-10在11:51

同意,但是在移植之前,调用ruby代码总比没有好。

– Dimid
16-10-10在12:59

它不是很复杂,在@recursive下面提供了可以使用的逻辑(只有几行代码)。

– yekta
16-10-10在13:00



在我看来,“十五十六”是错误的吗?

– PascalVKooten
16-10-29在10:21

@yekta是的,我认为递归的答案在SO答案的范围内很好。但是,gem提供了包含测试和其他功能的完整软件包。无论如何,我认为两者都有自己的位置。

– Dimid
16-10-29在16:37

#12 楼

利用python软件包:WordToDigits

pip install wordtodigits

它可以找到句子中以单词形式出现的数字,然后将其转换为正确的数字格式。如果存在的话,还要照顾小数部分。数字的单​​词表示形式可以在段落的任何地方。

https://pypi.org/project/wordtodigits/

#13 楼

一种快速的解决方案是使用inflect.py生成字典进行翻译。

inflect.py具有number_to_words()函数,该函数会将数字(例如2)转换为单词形式(例如'two')。不幸的是,没有提供它的反面(这将使您避免翻译词典的路线)。完全一样,您可以使用该函数来构建翻译词典:

>>> import inflect
>>> p = inflect.engine()
>>> word_to_number_mapping = {}
>>>
>>> for i in range(1, 100):
...     word_form = p.number_to_words(i)  # 1 -> 'one'
...     word_to_number_mapping[word_form] = i
...
>>> print word_to_number_mapping['one']
1
>>> print word_to_number_mapping['eleven']
11
>>> print word_to_number_mapping['forty-three']
43


如果您愿意花一些时间,则可以检查变形。 py的number_to_words()函数的内部工作原理,并构建自己的代码来动态执行此操作(我没有尝试执行此操作)。

#14 楼

我采用了@recursive的逻辑,并转换为Ruby。我还对查找表进行了硬编码,因此它不那么酷,但可能会帮助新手了解正在发生的事情。
WORDNUMS = {"zero"=> [1,0], "one"=> [1,1], "two"=> [1,2], "three"=> [1,3],
            "four"=> [1,4], "five"=> [1,5], "six"=> [1,6], "seven"=> [1,7], 
            "eight"=> [1,8], "nine"=> [1,9], "ten"=> [1,10], 
            "eleven"=> [1,11], "twelve"=> [1,12], "thirteen"=> [1,13], 
            "fourteen"=> [1,14], "fifteen"=> [1,15], "sixteen"=> [1,16], 
            "seventeen"=> [1,17], "eighteen"=> [1,18], "nineteen"=> [1,19], 
            "twenty"=> [1,20], "thirty" => [1,30], "forty" => [1,40], 
            "fifty" => [1,50], "sixty" => [1,60], "seventy" => [1,70], 
            "eighty" => [1,80], "ninety" => [1,90],
            "hundred" => [100,0], "thousand" => [1000,0], 
            "million" => [1000000, 0]}

def text_2_int(string)
  numberWords = string.gsub('-', ' ').split(/ /) - %w{and}
  current = result = 0
  numberWords.each do |word|
    scale, increment = WORDNUMS[word]
    current = current * scale + increment
    if scale > 100
      result += current
      current = 0
    end
  end
  return result + current
end

我正试图处理类似two thousand one hundred and forty-six的字符串

#15 楼

该代码适用于系列数据:
import pandas as pd
mylist = pd.Series(['one','two','three'])
mylist1 = []
for x in range(len(mylist)):
    mylist1.append(w2n.word_to_num(mylist[x]))
print(mylist1)


#16 楼

This code works only for numbers below 99.
both word to Int and int to word.
(for rest need to implement 10-20 lines of code and simple logic. This is just simple code for beginners)


num=input("Enter the number you want to convert : ")
mydict={'1': 'One', '2': 'Two', '3': 'Three', '4': 'Four', '5': 'Five','6': 'Six', '7': 'Seven', '8': 'Eight', '9': 'Nine', '10': 'Ten','11': 'Eleven', '12': 'Twelve', '13': 'Thirteen', '14': 'Fourteen', '15': 'Fifteen', '16': 'Sixteen', '17': 'Seventeen', '18': 'Eighteen', '19': 'Nineteen'}
mydict2=['','','Twenty','Thirty','Fourty','fifty','sixty','Seventy','Eighty','Ninty']
if num.isdigit():
    if(int(num)<20):
        print(" :---> "+mydict[num])
    else:
            var1=int(num)%10
            var2=int(num)/10
            print(" :---> "+mydict2[int(var2)]+mydict[str(var1)])
else:
    num=num.lower();
    dict_w={'one':1,'two':2,'three':3,'four':4,'five':5,'six':6,'seven':7,'eight':8,'nine':9,'ten':10,'eleven':11,'twelve':12,'thirteen':13,'fourteen':14,'fifteen':15,'sixteen':16,'seventeen':'17','eighteen':'18','nineteen':'19'}
    mydict2=['','','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninty']
    divide=num[num.find("ty")+2:]
    if num:
        if(num in dict_w.keys()):
            print(" :---> "+str(dict_w[num]))
        elif divide=='' :
                for i in range(0, len(mydict2)-1):
                   if mydict2[i] == num:
                      print(" :---> "+str(i*10))
        else :
            str3=0
            str1=num[num.find("ty")+2:]
            str2=num[:-len(str1)]
            for i in range(0, len(mydict2) ):
                if mydict2[i] == str2:
                    str3=i;
            if str2 not in mydict2:
                print("----->Invalid Input<-----")                
            else:
                try:
                    print(" :---> "+str((str3*10)+dict_w[str1]))
                except:
                    print("----->Invalid Input<-----")
    else:
            print("----->Please Enter Input<-----")


评论


请解释该代码的作用以及它的工作方式。这样一来,您的答案对那些还不太了解编码的人就更有价值了。

–Luuklag
17年8月21日在12:14

如果用户提供数字作为输入程序,则它将以单词形式返回,反之亦然,例如5-> 5,而对于5-> 5。程序适用于100以下的数字,但只需添加几行代码即可扩展到任意范围。

– Shriram Jadhav
17年6月6日在6:45