国产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
主站蜘蛛池模板: 日韩国产欧美视频 | 欧美一区三区 | 九九久久久2 | 久久精品国产99久久99久久久 | 人成精品视频三区二区一区 | 中文字幕综合 | 国产激情一区二区三区成人91 | 国产欧美一区二区 | 伊人精品久久久大香线蕉99 | 91久久精品国产91久久性色也 | 国产精品视频免费看 | 欧美在线国产 | 亚洲欧洲日韩在线 | 国产高清不卡码一区二区三区 | 一区二区在线观看高清 | 99久久精品国产综合一区 | 亚洲欧美精品伊人久久 | 亚欧精品在线观看 | 国产精品永久免费自在线观看 | 亚洲福利在线观看 | 欧美激情精品久久久久 | 国产激情视频一区二区三区 | xxx欧美888| 欧美国产日韩在线播放 | 亚洲乱码一二三四区麻豆 | 国产精品va一区二区三区 | 亚洲久草视频 | 亚洲欧美日韩一区 | 国产毛片久久久久久国产毛片 | 欧美在线不卡 | 91久久国产综合精品女同我 | 中文字幕日韩欧美 | 在线免费观看一区二区三区 | 国产欧美日韩精品综合 | 成人国产在线看不卡 | 欧美极品尤物在线播放一级 | 最新国产精品亚洲 | 国产在线视频专区 | 亚洲日本乱码中文论理在线电影 | 日本一二三区高清 | 亚洲欧美日韩色图 |