国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

python實現將英文單詞表示的數字轉換成阿拉伯數字的方法

來源:懂視網 責編:小采 時間:2020-11-27 14:34:13
文檔

python實現將英文單詞表示的數字轉換成阿拉伯數字的方法

python實現將英文單詞表示的數字轉換成阿拉伯數字的方法:本文實例講述了python實現將英文單詞表示的數字轉換成阿拉伯數字的方法。分享給大家供大家參考。具體實現方法如下: import re _known = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6
推薦度:
導讀python實現將英文單詞表示的數字轉換成阿拉伯數字的方法:本文實例講述了python實現將英文單詞表示的數字轉換成阿拉伯數字的方法。分享給大家供大家參考。具體實現方法如下: import re _known = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6

本文實例講述了python實現將英文單詞表示的數字轉換成阿拉伯數字的方法。分享給大家供大家參考。具體實現方法如下:

import re
_known = {
 '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
 }
def spoken_word_to_number(n):
 """Assume n is a positive integer".
assert _positive_integer_number('nine hundred') == 900
assert spoken_word_to_number('one hundred') == 100
assert spoken_word_to_number('eleven') == 11
assert spoken_word_to_number('twenty two') == 22
assert spoken_word_to_number('thirty-two') == 32
assert spoken_word_to_number('forty two') == 42
assert spoken_word_to_number('two hundred thirty two') == 232
assert spoken_word_to_number('two thirty two') == 232
assert spoken_word_to_number('nineteen hundred eighty nine') == 1989
assert spoken_word_to_number('nineteen eighty nine') == 1989
assert spoken_word_to_number('one thousand nine hundred and eighty nine') == 1989
assert spoken_word_to_number('nine eighty') == 980
assert spoken_word_to_number('nine two') == 92 # wont be able to convert this one
assert spoken_word_to_number('nine thousand nine hundred') == 9900
assert spoken_word_to_number('one thousand nine hundred one') == 1901
"""
 n = n.lower().strip()
 if n in _known:
 return _known[n]
 else:
 inputWordArr = re.split('[ -]', n)
 assert len(inputWordArr) > 1 #all single words are known
 #Check the pathological case where hundred is at the end or thousand is at end
 if inputWordArr[-1] == 'hundred':
 inputWordArr.append('zero')
 inputWordArr.append('zero')
 if inputWordArr[-1] == 'thousand':
 inputWordArr.append('zero')
 inputWordArr.append('zero')
 inputWordArr.append('zero')
 if inputWordArr[0] == 'hundred':
 inputWordArr.insert(0, 'one')
 if inputWordArr[0] == 'thousand':
 inputWordArr.insert(0, 'one')
 inputWordArr = [word for word in inputWordArr if word not in ['and', 'minus', 'negative']]
 currentPosition = 'unit'
 prevPosition = None
 output = 0
 for word in reversed(inputWordArr):
 if currentPosition == 'unit':
 number = _known[word]
 output += number
 if number > 9:
 currentPosition = 'hundred'
 else:
 currentPosition = 'ten'
 elif currentPosition == 'ten':
 if word != 'hundred':
 number = _known[word]
 if number < 10:
 output += number*10
 else:
 output += number
 #else: nothing special
 currentPosition = 'hundred'
 elif currentPosition == 'hundred':
 if word not in [ 'hundred', 'thousand']:
 number = _known[word]
 output += number*100
 currentPosition = 'thousand'
 elif word == 'thousand':
 currentPosition = 'thousand'
 else:
 currentPosition = 'hundred'
 elif currentPosition == 'thousand':
 assert word != 'hundred'
 if word != 'thousand':
 number = _known[word]
 output += number*1000
 else:
 assert "Can't be here" == None
 return(output)

希望本文所述對大家的Python程序設計有所幫助。

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

python實現將英文單詞表示的數字轉換成阿拉伯數字的方法

python實現將英文單詞表示的數字轉換成阿拉伯數字的方法:本文實例講述了python實現將英文單詞表示的數字轉換成阿拉伯數字的方法。分享給大家供大家參考。具體實現方法如下: import re _known = { 'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6
推薦度:
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产精品三级在线观看 | 最新国产区 | 欧美日韩亚洲无线码在线观看 | 国产a久久精品一区二区三区 | 免费一级| 欧美日本日韩aⅴ在线视频 欧美日韩91 | 亚洲国产综合久久精品 | 黄色在线观看免费 | 欧美射| 国产成人精品一区二三区 | 久久久这里有精品999 | 在线观看精品一区 | 最新国产在线 | 国内精品久久久久影院不卡 | 2020年国产高中毛片在线视频 | 国内精品一区二区三区 | 欧美福利在线 | 99精品视频在线观看免费 | 国产欧美曰韩一区二区三区 | 日韩在线视频网 | 在线 v亚洲 v欧美v 专区 | 欧美日韩视频一区二区 | 精品伊人久久久久7777人 | 九九精品视频一区在线 | 成人毛片一区二区三区 | 欧美色图网站 | 另类在线 | 日韩专区在线观看 | 亚洲色图 欧美 | 在线观看亚洲欧美 | 自拍亚洲| 精品国产一二三区在线影院 | 视频二区 素人 欧美 日韩 | 日韩中文字幕免费版 | 999久久久| 欧美亚洲免费 | 欧美日在线观看 | 久久久久久久99久久久毒国产 | 久久精品国产一区二区三区日韩 | 天码毛片一区二区三区入口 | 久久这里只有精品9 |