简体   繁体   English

如何在python中将字符串转换为数字

[英]How to convert string into numbers in python

I see an interesting python exercise in codewars.it is about convert strings to numbers.I want some suggestions or guidance to solve this python exercise.Thank you我在 codewars 中看到一个有趣的 python 练习。它是关于将字符串转换为数字。我想要一些建议或指导来解决这个 python 练习。谢谢

this is the exercise:In this kata we want to convert a string into an integer.这是练习:在这个 kata 中,我们想将字符串转换为整数。 The strings simply represent the numbers in words.字符串只是用单词表示数字。 Examples: "one"1示例:“一个”1

and this is my code:这是我的代码:

def parse_int(string):
    dict_of_numbers={ "zero":0, "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, "twenty":20, "thirty":30, "forty":40, "fifty":50, "sixty":60, "seventy":70, "eighty":80, "ninety":90,"thousand":1000,"hundred":100}

    string=string.replace(' ','-')
    numbers=string.split('-')
    created_number=0
    for number in numbers:
        for key,value in dict_of_numbers.items():
            if number==key:
                created_number+=value
    return created_number

I have a solution and I did not test it for large collection of numbers but it may give you some ideas:我有一个解决方案,我没有针对大量数字对其进行测试,但它可能会给您一些想法:

  1. Spoken numbers sometimes are summed sometimes are multiplied.口语有时相加有时相乘。
  2. Some times people put an and between numbers.有时人们会在数字之间加上and Like: thirty seven thousand and twenty one喜欢: thirty seven thousand and twenty one
  3. Don't use [] to get value from your dictionary.不要使用[]从您的字典中获取价值。 Use get method.使用get方法。 So if there is not data corresponding to the number you have control over the return.因此,如果没有与数字对应的数据,则您可以控制退货。
  4. use str's .lower() to lower the letters in the string to avoid upper case, lower case problem使用str的.lower()降低字符串中的字母,避免大写、小写问题

The code I wrote would look like:我写的代码看起来像:

def parse_int(string):
    dict_of_numbers = {"zero": 0, "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, "twenty": 20,
                       "thirty": 30, "forty": 40, "fifty": 50, "sixty": 60, "seventy": 70, "eighty": 80, "ninety": 90,
                       "thousand": 1000, "hundred": 100}

    string = string.replace(" and ", " ")
    the_number = 0
    for each in string.lower().split():
        if each in ["hundred", "thousand"]:
            the_number *= dict_of_numbers.get(each, 1)
        else:
            the_number += dict_of_numbers.get(each, 0)


    return the_number


print(parse_int("thirty seven thousand and twenty two")) # 37022

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM