字符串操作执行不正确

Eri*_*aus 4 python python-3.x

main.py我有这个

import RemoveShortWords as rs

procLine="the   in  shear flow past a flat plate"
procLine = rs.RemomeOneTwoCharWords(procLine)
print(procLine)
Run Code Online (Sandbox Code Playgroud)

RemoveShortWords.py这是

def RemomeOneTwoCharWords(procLine):

    procLine = str(procLine)

    for word in procLine.split():

        if(len(word)<=2):
            procLine = procLine.replace(word,"")


    return procLine
Run Code Online (Sandbox Code Playgroud)

print返回此信息

sher flow pst flt plte

正如您所看到的,函数会删除少于2个字符的单词.但是,出于某种原因,它也删除了所有"a"字符.例如,"flat"变成了"flt"

为什么?

stu*_*ent 6

变量procLinethe in shear flow past a flat plate

并且procLine.split() [''','in','剪切','流','过去','a','平','板']

for一个循环中,找到a并替换aprocLine用空或删除字符.注意:如果有一个单词与in例如stringto,它会做同样的事情strg.

相反,我会建议list comprehension可能有用的东西:

procLine = ' '.join([w for w in procLine.split() if len(w)>2])
Run Code Online (Sandbox Code Playgroud)