我需要转换one成1,two转入2等等.
有没有办法用库或类或任何东西来做到这一点?
代码高尔夫系列似乎相当受欢迎.我遇到了一些将数字转换为单词表示的代码.一些例子是(编程乐趣的2的权力):
我的同事出现的算法差不多有两百行.似乎会有更简洁的方法来做到这一点.
现行指南:
我需要将整数转换为python中的英文(字符串)表示.例:
5 = five
20 = twenty
etc.
Run Code Online (Sandbox Code Playgroud)
我知道,我可以使用条件,但我需要转换许多整数,所以我需要以最快的方式来实现它.
编写一个以整数作为输入参数的函数,并使用单词返回整数.例如,如果输入是4721,那么函数应该返回字符串"four seven two one".请注意,单词之间应该只有一个空格,并且它们应该在您返回的字符串中全部小写.
这是我的代码:
def Numbers_To_Words (number):
dict = {1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 0: "zero"}
output_str = " "
list = []
#Main Program
number = 4721
result = Numbers_To_Words (number)
print (result)
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何将数字分开,然后与我创建的字典进行比较?我知道长度不适用于整数数据类型.我知道将密钥发送到字典并获取其各自值的进一步逻辑.但在此之前,我被困在这里分隔整数的数字.
可能重复:
如何告诉Python将整数转换为单词
我正在尝试编写一个简单的函数,它将输入作为整数,然后将其显示为单词.我不确定如何正确地说出这个问题.这是一个时钟应用程序,这就是我正在做的,但我相信有一个更简单的方法.
if h == 1: h = "One"
if h == 2: h = "Two"
if h == 3: h = "Three"
if h == 4: h = "Four"
if h == 5: h = "Five"
if h == 6: h = "Six"
if h == 7: h = "Seven"
if h == 8: h = "Eight"
if h == 9: h = "Nine"
if h == 10: h = "Ten"
if h == 11: h = "Eleven" …Run Code Online (Sandbox Code Playgroud) 如果不使用库函数,如何在Python中打印任意数量的单词?有一些答案是使用库函数,但我想要核心代码.
Like:
12345 = "twelve thousand three hundred and forty five"
97835200 ="Nine core seventy eight lakh thirty five thousand two hundred"
230100 = "Two lakh thirty thousand one hundred"
Run Code Online (Sandbox Code Playgroud) python ×5
integer ×3
string ×2
code-golf ×1
nlp ×1
numbers ×1
python-2.7 ×1
python-3.x ×1
text ×1