如果不使用库函数,如何在 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"
如果不使用库函数,如何在 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"
代码:
>>>def handel_upto_99(number):
predef={0:"zero",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",20:"twenty",30:"thirty",40:"forty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety",100:"hundred",100000:"lakh",10000000:"crore",1000000:"million",1000000000:"billion"}
if number in predef.keys():
return predef[number]
else:
return predef[(number/10)*10]+' '+predef[number%10]
>>>def return_bigdigit(number,devideby):
predef={0:"zero",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",20:"twenty",30:"thirty",40:"forty",50:"fifty",60:"sixty",70:"seventy",80:"eighty",90:"ninety",100:"hundred",1000:"thousand",100000:"lakh",10000000:"crore",1000000:"million",1000000000:"billion"}
if devideby in predef.keys():
return predef[number/devideby]+" "+predef[devideby]
else:
devideby/=10
return handel_upto_99(number/devideby)+" "+predef[devideby]
>>>def mainfunction(number):
dev={100:"hundred",1000:"thousand",100000:"lakh",10000000:"crore",1000000000:"billion"}
if number is 0:
return "Zero"
if number<100:
result=handel_upto_99(number)
else:
result=""
while number>=100:
devideby=1
length=len(str(number))
for i in range(length-1):
devideby*=10
if number%devideby==0:
if devideby in dev:
return handel_upto_99(number/devideby)+" "+ dev[devideby]
else:
return handel_upto_99(number/(devideby/10))+" "+ dev[devideby/10]
res=return_bigdigit(number,devideby)
result=result+' '+res
if devideby not in dev:
number=number-((devideby/10)*(number/(devideby/10)))
number=number-devideby*(number/devideby)
if number <100:
result = result + ' '+ handel_upto_99(number)
return result
将这三个函数一一复制并粘贴到您的 python shell 中。之后像这样运行:
回答:
>>>mainfunction(12345)
' twelve thousand three hundred forty five'
>>>mainfunction(0)
'Zero'
>>>mainfunction(100)
'one hundred'
>>>mainfunction(40230534)
' four crore two lakh thirty thousand five hundred thirty four'
您可以使用 python 中可用的第三方库 num2word
num2word.to_card(1e25)
'ten septillion, one billion, seventy-three million, seven hundred and forty-one
this will avoid your long code and you can directly use it.
下面是一个可以将数字转换为单词的函数。它使用数字的标准英文名称,但如果需要,您可以将其修改为您的特殊名称。此函数最多可以处理 10^60 个数字。通过调用函数来使用它: int2word(n) 其中 n 是数字
def int2word(n):
"""
convert an integer number n into a string of english words
"""
# break the number into groups of 3 digits using slicing
# each group representing hundred, thousand, million, billion, ...
n3 = []
r1 = ""
# create numeric string
ns = str(n)
for k in range(3, 33, 3):
r = ns[-k:]
q = len(ns) - k
# break if end of ns has been reached
if q < -2:
break
else:
if q >= 0:
n3.append(int(r[:3]))
elif q >= -1:
n3.append(int(r[:2]))
elif q >= -2:
n3.append(int(r[:1]))
r1 = r
#print n3 # test
# break each group of 3 digits into
# ones, tens/twenties, hundreds
# and form a string
nw = ""
for i, x in enumerate(n3):
b1 = x % 10
b2 = (x % 100)//10
b3 = (x % 1000)//100
#print b1, b2, b3 # test
if x == 0:
continue # skip
else:
t = thousands[i]
if b2 == 0:
nw = ones[b1] + t + nw
elif b2 == 1:
nw = tens[b1] + t + nw
elif b2 > 1:
nw = twenties[b2] + ones[b1] + t + nw
if b3 > 0:
nw = ones[b3] + "hundred " + nw
return nw
'''Global'''
ones = ["", "one ","two ","three ","four ", "five ",
"six ","seven ","eight ","nine "]
tens = ["ten ","eleven ","twelve ","thirteen ", "fourteen ",
"fifteen ","sixteen ","seventeen ","eighteen ","nineteen "]
twenties = ["","","twenty ","thirty ","forty ",
"fifty ","sixty ","seventy ","eighty ","ninety "]
thousands = ["","thousand ","million ", "billion ", "trillion ",
"quadrillion ", "quintillion ", "sextillion ", "septillion ","octillion ",
"nonillion ", "decillion ", "undecillion ", "duodecillion ", "tredecillion ",
"quattuordecillion ", "sexdecillion ", "septendecillion ", "octodecillion ",
"novemdecillion ", "vigintillion "]