从字符串中提取数字并替换?

2024-09-29 23:27:49 发布

您现在位置:Python中文网/ 问答频道 /正文

所以现在,根据我所知,我的代码正在按预期工作。但是,我无法将一个小模块或其他几行放在一起,从用户输入中提取整数并将其膨胀1。你知道吗

dictZero = [ "zero", "none", "null", "nil" ]
dictOne = [ "juan", "one", "won" ]
dictTwo = [ "two", "to", "too", "tu" ]
dictThree = [ "three" ]
dictFour = [ "four", "fore", "for" ]

userInput = input ( "Enter your sentence to inflate: " )


for i in userInput.split():              
    for e in dictFour:
        if e in i:
            userInput = userInput.replace ( e, "five" )                
    for d in dictThree:
        if d in i:
            userInput = userInput.replace ( d, "four" )                
    for c in dictTwo:
        if c in i:
            userInput = userInput.replace ( c, "three" )                
    for b in dictOne:
        if b in i:
            userInput = userInput.replace ( b, "two" )                
    for a in dictZero:
        if a in i:
            userInput = userInput.replace ( a, "one" )


print ( userInput )

样本输入:

Before we begin to do anything at 1630.

样本输出:

Befive we begin three do anything at 1631.

在不过分复杂化和显著更改代码的情况下,我可以简单地+1输入字符串中的任何数字吗?你知道吗


Tags: to代码inforifonereplacethree
2条回答

标准不清楚,我会这样做。你知道吗

import re

word_dict = dict.fromkeys(["zero", "none", "null", "nil"], 'one')
word_dict.update(dict.fromkeys([ "juan", "one", "won" ], 'two'))
word_dict.update(dict.fromkeys([ "two", "to", "too", "tu" ], 'three'))
word_dict.update(dict.fromkeys([ "three" ], 'four'))
word_dict.update(dict.fromkeys([ "four", "fore", "for" ], 'five'))

userInput = input ( "Enter your sentence to inflate: " )

for i in word_dict.keys():
    userInput = re.sub(i, word_dict[i], userInput)
digits = re.compile(r'\d+')
userInput = digits.sub(lambda m: str(int(m.group(0)) + 1), userInput)

print ( userInput )

运行示例:

Enter your sentence to inflate: 'Before we begin to do anything at 1630.'

Befivee we begin three do anything at 1631.

如果忽略句末的1630str.replace只需替换整行的单词,而无需按单词进行拆分。你知道吗

如果您还想添加十进制数,则需要逐个字符,这将增加代码的复杂性。你知道吗

dictZero = [ "zero", "none", "null", "nil" ]
dictOne = [ "juan", "one", "won" ]
dictTwo = [ "two", "to", "too", "tu" ]
dictThree = [ "three" ]
dictFour = [ "four", "fore", "for" ]

userInput = input ( "Enter your sentence to inflate: " )

for i in dictFour:
    userInput = userInput.replace(i, 'five')
for i in dictThree:
    userInput = userInput.replace(i, 'four')
for i in dictTwo:
    userInput = userInput.replace(i, 'three')
for i in dictOne:
    userInput = userInput.replace(i, 'two')
for i in dictZero:
    userInput = userInput.replace(i, 'one')

output = ''
num = ''
for c in userInput:  # Going char by char to find decimal values
    if c.isdigit():  # is this char a digit?
        num += c  # if so remember it
    else:
        if num: # if we just found a whole number
            output += str(int(num) + 1) # add 1 and append the string
        num = ''
        output += c  # Append any non-decimal character


print(output)

输入:

Before we begin to do anything at 1630.

输出:

Befive we begin three do anything at 1631.

请注意,这不会在字符串中添加float或负值,只添加ints

相关问题 更多 >

    热门问题