国产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實現(xiàn)將英文單詞表示的數(shù)字轉換成阿拉伯數(shù)字的方法

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

python實現(xiàn)將英文單詞表示的數(shù)字轉換成阿拉伯數(shù)字的方法

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

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

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程序設計有所幫助。

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

文檔

python實現(xiàn)將英文單詞表示的數(shù)字轉換成阿拉伯數(shù)字的方法

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

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 欧美亚洲另类在线观看 | 国产全黄a一级毛片视频 | 日韩av线上 | 福利毛片 | 香蕉乱码成人久久天堂爱免费 | 久久国产欧美日韩高清专区 | 亚洲欧美日韩在线观看播放 | 日韩欧美网址 | 国产真实乱人视频在线看 | 四虎精品永久在线 | a男人的天堂久久a毛片 | 性欧美嫩交hd | 国产精品一区二区三区免费 | 永久免费毛片 | 日本五十路视频 | 免费一区二区三区免费视频 | 中文字幕日韩一区二区三区不卡 | 国产精品九九 | 91精品一区二区三区在线观看 | 国产免费视屏 | 欧美精品日韩一区二区三区 | 日本三级韩国三级欧美三级 | 最新国产精品视频免费看 | 久久亚洲一级α片 | 久久无码av三级 | 国产亚洲欧美日韩综合另类 | 另类区| 欧美日韩亚洲高清不卡一区二区三区 | 国产精品亚洲四区在线观看 | 日韩高清一区 | 国产成人深夜福利短视频99 | 国产精品久久久久久久成人午夜 | 国产在线精品一区二区高清不卡 | 欧美 日韩 国产 色 欧美 日韩 中文 | 日本精品久久久一区二区三区 | 欧美另类色图 | 欧美成人h精品网站 | 国产只有精品 | 欧美日韩高清完整版在线观看免费 | 亚洲欧美日韩成人 | 欧美在线视频在线观看 |