将字符串数学指令转换为其等效运算
我想将字符串数学指令转换为等效整数操作;
例如: 1.'double三'= 33
- '三重六'= 666
我的代码是:
hashmap={
'one': '1',
'two': '2',
'three': '3',
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9',
'zero' : '0'}
str1="one five three"
st2int = ''.join(hashmap[ele] for ele in str1.split())
print(st2int)
我的程序仅适用于str
数字。 我该如何使其用于指导,例如double,triple,Quadraple
等,如我所述
i want to convert a string mathematical instruction into it's equivalent integer operation;
eg:
1.'double three'= 33
- 'triple six'=666
my code is:
hashmap={
'one': '1',
'two': '2',
'three': '3',
'four': '4',
'five': '5',
'six': '6',
'seven': '7',
'eight': '8',
'nine': '9',
'zero' : '0'}
str1="one five three"
st2int = ''.join(hashmap[ele] for ele in str1.split())
print(st2int)
my program is only for the str
numbers to integer..
how can i make it to work for instruction like double,triple,quadraple
etc as i mentioned in the example
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为乘数和数字创建一个单独的字典。如果某个单词在乘数词典中,请记住它的乘数值是多少。如果它在数字字典中,则乘以当前的乘数。
这打印:
You make a separate dict for multipliers and for digits. If a word is in the multipliers dictionary, remember what its multiplier value is. If it's in the digits dictionary, multiply by the current multiplier.
This prints: