【问题标题】:Python - Remove whitespaces and punctuation without functionsPython - 删除没有功能的空格和标点符号
【发布时间】:2013-11-18 13:06:11
【问题描述】:

首先,对不起我的英语不好。我是一个初学者程序员,我的 python 程序有一些问题。 我必须制作一个规范化空格和标点符号的程序,例如:

如果我把一个字符串叫做

"   hello   how,   are  u?   "

新字符串必须是...

"Hello how are u"

但在我的代码中,结果是这样的,我不知道为什么:

 "helloo how,, aree u??"

注意:我不能使用任何类型的函数,如 split()、strip() 等...

这是我的代码:

from string import punctuation

print("Introduce your string: ")
string = input() + " "
word = ""
new_word = ""
final_string = ""

#This is the main code for the program
for i in range(0, len(string)):
    if (string[i] != " " and (string[i+1] != " " or string[i+1] != punctuation)):
        word += string[i]
    if (string[i] != " " and (string[i+1] == " " or string[i+1] == punctuation)):
        word += string[i] + " "
        new_word += word
        word = ""

#This destroys the last whitespace
for j in range(0,len(new_word)-1):
    final_string += new_word[j]

print(final_string)

谢谢大家。

编辑:

现在我有了这个代码:

letter = False

for element in my_string:
    if (element != " " and element != punctuation):
        letter= True
        word += element


print(word)

但是现在,问题是我的程序无法识别标点符号,所以如果我输入:

"Hello   ... how  are u?"

必须像"Hellohowareu"

但它是这样的:

"Hello...howareu?

【问题讨论】:

  • 你能用translate吗?
  • 我不能使用任何函数或那个,我只能使用 string.punctuation 和基本代码,如 for、if、while 等......
  • 但你想去掉字符串的开头和结尾?
  • 是的,我不想在所有字符串的开头或结尾出现任何标点符号或空格
  • string[i+1] != [/==] punctuationstring[i+1]整个 punctuation 字符串进行比较。那永远行不通。你想要string[i+1] in punctuation(如果punctuationset,则更快),但要注意' 是标点符号,所以像don't 这样的词会很麻烦。

标签: python function space punctuation


【解决方案1】:

我不会为你写代码,因为这显然是家庭作业,但我会给你一些提示。

我认为您检查下一个字符的方法有点容易出错。相反,我会在您看到空格或标点符号时设置一个标志。下一次循环,检查标志是否设置:如果是,并且你仍然看到一个空格,那么忽略它,否则,将标志重置为 false。

【讨论】:

    【解决方案2】:

    好的,首先,您不需要遍历范围,python 中的字符串是可迭代的。例如:

    my_string = 'How are you?'
    
    for char in my_string:
      #do something each character
    

    其次,您对要删除的内容使用了非常参差不齐的方法。似乎您捕获字符后出现的空格的方法会导致最后一个字符的双重附加。我会使用一种不同的方法,更侧重于你所在的位置,而不是你面前的东西。

    【讨论】:

    • 嗨@DylanLawrence,感谢您的回复,但我还有另一个问题。它似乎无法识别标点符号,程序将其附加到最后一个单词。 letter = False; for element in my_String: if element != " " and element != punctuation: letter = True; word += element; print(word);理论上如果我输入“你好,你好吗?”。它必须是“Hellohowareu”但它是“Hellohowareu?”
    【解决方案3】:

    现在这看起来很像家庭作业,所以这是我的流处理解决方案,如果你能向你的老师解释一下,我怀疑他们会介意你不是真的自己做的?

    def filter(inp):
        for i in inp:
            yield " " if i in " ,.?!;:" else i
    
    def expand(inp):
        for i in inp:
            yield None if i == " " else object(), i
    
    def uniq(inp):
        last = object()
        for key, i in inp:
            if key == last:
                continue
            yield key, i
    
    def compact(inp):
        for key, i in inp:
            yield i
    
    normalised = compact(uniq(expand(filter(input()))))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-14
      • 2021-12-20
      • 1970-01-01
      • 2019-08-25
      • 2019-05-09
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多