Python 在不使用拆分函数的情况下删除字符串中的空格
需要删除字符串中所有多余的空格,包括开头和结尾的空格。我不能使用拆分功能。仅当和while语句。到目前为止,我已经有了它,但每次我运行它时,它只会以完全相同的方式返回输入Python 在不使用拆分函数的情况下删除字符串中的空格,python,split,Python,Split,需要删除字符串中所有多余的空格,包括开头和结尾的空格。我不能使用拆分功能。仅当和while语句。到目前为止,我已经有了它,但每次我运行它时,它只会以完全相同的方式返回输入 def cleanstring(S): i=0 startpos=0 endpos=0 end=-1 word=0 #find position of first letter while S[i]==(" "): i=i+0 startpos=i #find last l
def cleanstring(S):
i=0
startpos=0
endpos=0
end=-1
word=0
#find position of first letter
while S[i]==(" "):
i=i+0
startpos=i
#find last letter
while (S[end]==(" ")):
end=end-1
endpos=S[len(S)-end]
#make first letter found the first letter in the string
if S[i]!=(" "):
word=S[i]
#start between startpos and endpos to find word
while (i<endpos) and (i>startpos):
while S[i]!=(" "):
word=word+S[i]
if S[i]==(" "):
if (S[i+1]==("")) or (S[i-1]==(" ")):
word=word+(" ")
else:
word=word+(" ")
#return the word
print(word)
Input=[" Hello to the world "]
def清洗字符串:
i=0
startpos=0
endpos=0
结束=-1
字=0
#找到第一个字母的位置
而S[i]==(“”):
i=i+0
startpos=i
#找到最后一封信
而(S[end]==(“”):
end=end-1
endpos=S[len(S)-结束]
#使第一个字母在字符串中找到第一个字母
如果他[我]=(" "):
word=S[i]
#从startpos和endpos之间开始查找单词
而(istartpos):
而S[我]=(" "):
word=word+S[i]
如果S[i]==(“”):
如果(S[i+1]==(“”)或(S[i-1]==(“”)):
word=word+(“”)
其他:
word=word+(“”)
#回话
打印(word)
输入=[“向世界问好”]
如果使用带有空格的字符串调用cleanstring函数,这将导致无限循环:
while S[i]==(" "):
i=i+0
startpos=i
既然你给i加零,它就永远不会改变。您应该将其增加1,可以这样做:
i += 1
它是
i = i + 1
然而,输入甚至不是一个字符串,而是一个包含字符串的列表。您应该将输入表达式更改为
Input = " Hello to the world "
您使用的方括号将其列为一个包含字符串的列表。使用
for
:
def cleanstring(str_in):
str_out = ''
last_char = None
for cur_char in str_in:
str_out += '' if last_char == ' ' and cur_char ==' ' else cur_char
last_char = cur_char
return str_out
在时使用:
def cleanstring(str_in):
str_out = ''
last_char = None
index = 0
while str_in[index:index+1]:
cur_char = str_in[index:index+1]
str_out += '' if last_char == ' ' and cur_char ==' ' else cur_char
last_char = cur_char
index+=1
return str_out
如果最后一个字符和当前字符是空格,则不要附加空格
我们假设空格是唯一相关的空白。否则,这是一个针对空白集的解决方案:
def cleanstring(str_in):
str_out = ''
last_char = None
index = 0
whitespace = [' ','\t','\n','\r','\f','\v']
while str_in[index:index+1]:
a = str_in[index:index+1]
str_out += '' if last_char in whitespace and a in whitespace else a
last_char = a
index+=1
return str_out
这将删除除第一个检测到的条目之外的所有空格,但是如果我们要删除与相邻空格类似的空格并保留第一个检测到的实例:
def cleanstring(str_in):
str_out = ''
last_char = None
index = 0
whitespace = [' ','\t','\n','\r','\f','\v']
while str_in[index:index+1]:
a = str_in[index:index+1]
str_out += '' if last_char == a and a in whitespace else a
last_char = a
index+=1
return str_out
如果您担心在
中的使用,可以将其替换为(以cleanstring
的最后一个实例为例):
最后一个示例的空格如下所示:
第73-74行
我知道这可能不是最符合Pythonic或PEP8的,请随意编辑它。Concat当您转到临时字符串时,如果您点击空格字符,请检查临时字符串是否为空,如果不是,则生成它并重置临时字符串
s = " Hello to the world "
def split(s):
temp_s = ""
for ch in s:
if ch.isspace():
if temp_s:
yield temp_s
temp_s = ""
else:
temp_s += ch
if temp_s:
yield temp_s
输出:
In [5]: s = " Hello to the world "
In [6]: list(split(s))
Out[6]: ['Hello', 'to', 'the', 'world']
In [7]: s = " Hello\tto\r\nthe world "
In [8]: list(split(s))
Out[8]: ['Hello', 'to', 'the', 'world']
In [10]: list(split(s))
Out[10]: ['Hello', 'world']
In [11]: s = "Hello"
In [12]: list(split(s))
Out[12]: ['Hello']
显然,如果需要,您可以将for更改为while循环。请阅读下面的评论
TABLE = str.maketrans('','',' \n\r\t\f')
def clrstr(inp):
return inp.translate(TABLE)
但是,如果您正在学习while和for循环,则没有多大帮助。只需使用string.strip()
方法。这是一种家庭作业还是什么?
如果不能使用“for”,只能使用“If”和“while”,那么我会使用计数器检查字符串中的每个字符
def clean(input):
idx = 0
out = input[idx]
while idx < len(input):
if input[idx] != out[-1] or input[idx] != ' ':
out += input[idx]
idx+=1
return out
def清洁(输入):
idx=0
输出=输入[idx]
当idx
当然,这不是完整的解决方案,但你明白了。空白
实际上比空格('
)要宽一些,但也包括\t
和\n
——你是想删除多余的空白还是仅仅是多余的空白?多余的空白,所以在每个单词之间留空格,我的问题是你所说的“空白”一词是什么意思。您的代码只是查看空格,而忽略制表符和换行符(根据该术语的标准定义,它们也算作空白)。这是出于设计,还是代码中的一个bug?删除是什么意思?输出应该是什么?输出应该是“Hello to the world”(向世界问好)。即使你做了这两件事,你的函数也不会工作,但它会让你走得更远。我建议使用print语句查看您的值,以帮助您跟踪代码。需要一段时间才能掌握诀窍,但是继续尝试,你会得到它。这段代码不分割地删除所有空白,因为这是问题中提出的问题。修改的目的是删除我忽略的评论中多余的空格。
TABLE = str.maketrans('','',' \n\r\t\f')
def clrstr(inp):
return inp.translate(TABLE)
def clean(input):
idx = 0
out = input[idx]
while idx < len(input):
if input[idx] != out[-1] or input[idx] != ' ':
out += input[idx]
idx+=1
return out