一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python實現(xiàn)將英文單詞表示的數(shù)字轉(zhuǎn)換成阿拉伯數(shù)字的方法

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

2020-07-19 10:39秋風秋雨 Python

這篇文章主要介紹了python實現(xiàn)將英文單詞表示的數(shù)字轉(zhuǎn)換成阿拉伯數(shù)字的方法,涉及Python字符串轉(zhuǎn)換操作的相關(guān)技巧,需要的朋友可以參考下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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程序設(shè)計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色天使亚洲综合在线观看 | 国产精品久久久久久久牛牛 | 亚洲高清在线天堂精品 | 狠狠色狠狠色综合日日小蛇 | 网友偷自拍原创区 | 美琪美腿白丝交小说 | 91丝袜足控免费网站xx | 四虎最新永久在线精品免费 | 草莓香蕉绿巨人丝瓜榴莲污在线观看 | 日本午夜vr影院新入口 | 千金奴隶在线 | 久久精品人人做人人爽97 | 天天综合色天天综合 | 91小视频在线观看免费版高清 | 波多野结衣中文字幕乱七八糟 | 色屁屁www | 国产日韩一区二区 | 久久久91精品国产一区二区 | www.麻豆| 国产xxxxxx久色视频在 | 猛h辣h高h文湿校园1v1 | 九九九国产在线 | 人人最怕九月羊 | 日韩欧美中文字幕一区二区三区 | 日日操日日舔 | 亚洲一区二区三区久久精品 | 久久r视频| 国产日本免费 | 免费一区二区视频 | 成熟女人50岁一级毛片不卡 | 东京干福利 | 非洲黑人又大粗gay 非洲黑人bbwbbwbbw | www.午夜剧场| 午夜一个人在线观看完整版 | 99久久精品免费看国产高清 | h网站国产 | 69日本人| 无限资源在线观看播放 | 美女翘臀跪床被打屁股作文 | 亚洲h片 | 四虎影院2022 |