清理没有拆分/剥离/内置功能的字符串 [英] Cleaning up a string without split/strip/built-in functions

查看:84
本文介绍了清理没有拆分/剥离/内置功能的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求

使用Python创建函数cleanstring(S)来清理"句子S中的空格.

Use Python to create a function cleanstring(S) to "clean up" the spaces in a sentence S.

  • 句子的开头和/或结尾和/或单词之间可能有多余的空格.
  • 该子例程返回句子的新版本,不带多余的空格.
    • 也就是说,在新字符串中,单词应该相同,但开头不应有空格,每个单词之间仅应有一个空格,而结尾应无空格.
    • The sentence may have extra spaces at the front and/or at the end and/or between words.
    • The subroutine returns a new version of the sentence without the extra spaces.
      • That is, in the new string, the words should be the same but there should be no spaces at the start, only one space between each word and no spaces at the end.

      该程序与您编写代码以搜索字符串以查找单词有关,因此您不可以在Python中使用split函数.

      This program is about you writing code to search through a string to find words and so you are not allowed to use the split function in Python.

      您可以使用if和while语句的基本功能以及len和concatentation的字符串操作来解决此问题.

      You can solve this problem with the basic capabilities of the if and while statements and string operations of len and concatentation.

      例如:如果输入是:世界你好!"那么输出应该是:世界你好!"

      For example: if the input is: " Hello to the world !" then the output should be: "Hello to the world!"

      问题

      我的程序删除了程序中多余的字符.

      My program deletes more characters in the program than needed.

      输入:"Hello World!"

      Input: " Hello World ! "

      输出:"HellWorl"

      Output: "HellWorl"

      如何解决程序中的错误?

      How do I fix the error in my program?

      def cleanupstring (S):
          newstring = ["", 0]
          j = 1
          for i in range(len(S) - 1):
              if S[i] != " " and S[i+1] != " ":
                  newstring[0] = newstring[0] + S[i]
              else:
                  newstring[1] = newstring [1] + 1
          return newstring
      
      # main program
      
      sentence = input("Enter a string: ")
      
      outputList = cleanupstring(sentence)
      
      print("A total of", outputList[1], "characters have been removed from your 
      string.")
      print("The new string is:", outputList[0]) 
      

      推荐答案

      欢迎使用Stackoverflow.当我开始阅读时,虽然这将是一个请回答我的作业"问题,但实际上您在解决问题上已经做出了相当大的努力,因此,我很乐于尝试并提供帮助(只有您可以说是否我实际上是这样做的.)

      Welcome to Stackoverflow. When I started reading I though this was going to be a "please answer my homework" question, but you've actually made a pretty fair effort at solving the problem, so I'm happy to try and help (only you can say whether I actually do).

      当您学习一种新语言时,有时很难放弃其他语言更合适的技术.通常一个字符一个字符地执行操作,通常只使用for c in s而不是像在C语言中那样递增索引值(尽管这两种方法都行得通,但有时不需要的索引递增有时被视为"unpythonic").您的基本想法似乎是先检测一个空格,再检测另一个空格,否则将字符从输入复制到输出.

      It's sometimes difficult when you are learning a new language to drop techniques that are much more appropriate in other languages. Doing it character by character you normally just use for c in s rather than incrementing index values like you would in C (though either approach works, index incrementation where not necessary is sometimes regarded as "unpythonic"). Your basic idea seems to be to detect a space followed by another space, otherwise copying characters from the input to the output.

      可以通过保留发送到输出的最后一个字符来简化逻辑.如果是空格,请不要再发送其他空格.前面的一个循环消除了任何前导空格,并且由于末尾最多可以有一个空格,因此如果存在的话可以很容易地消除它.

      The logic can be simplified by retaining the last character you sent to the output. If it's a space, don't send any more spaces. A loop at the front gets rid of any leading spaces, and since there can be at most one space at the end it can be eliminated easily if present.

      我不确定为什么要使用列表来保留结果,因为这会使代码更难以理解.如果您需要返回多条信息,则在单个变量中计算它们然后在return语句中构造结果要容易得多.

      I'm not sure why you use a list to keep your results in, as it makes the code much more difficult to understand. If you need to return multiple pieces of information it's much easier to compute them in individual variables and then construct the result in the return statement.

      因此,一种理想的修改是将newstring[0]替换为out_s,将newstring[1]替换为count.这样可以使事情更加清楚.然后,如果确实需要列表,请在return [out_s, count]结尾.使用return out_s, count的元组会更常见.

      So one desirable modification would be to replace newstring[0] with, say, out_s and newstring[1] with, say count. That will make it a bit clearer what's going on. Then at the end return [out_s, count] if you really need a list. A tuple using return out_s, count would be more usual.

      def cleanupstring (s):
          out_s = ''
          count = 0
          last_out = ' '
          for c in s:
              if c != ' ' or last_out != ' ':
                  last_out = c
                  out_s += c
              else:
                  count += 1
          if last_out == ' ':
              count -= 1
              out_s = out_s[:-1]
          return out_s, count
      
      # main program
      
      sentence = input("Enter a string: ")
      
      outputList = cleanupstring(sentence)
      
      print("A total of", outputList[1], "characters have been removed from your string.")
      print("The new string is:", outputList[0])
      

      有时候,您只是没有某些信息可以帮助您非常简洁地回答问题.您很可能还没有听说过stripreplace方法,所以我想像一下以下(未经测试的)代码

      Sometimes you just don't have certain pieces of information that would help you to answer the question extremely succinctly. You most likely haven't yet been taught about the strip and replace methods, and so I imagine the following (untested) code

      def cleanupstring(s):
          out_s = s
          while '  ' in out_s:
              out_s = out_s.strip().replace('  ', ' ')
          return out_s, len(s)-len(out_s)
      

      马上就解决.

      此外,您可以使用拆包分配"将功能输出的不同元素直接通过写操作绑定到名称上

      Also, you can use an "unpacking assignment" to bind the different elements of the function's output directly to names by writing

      s, c = cleanupstring(...)
      

      我确定你会同意

      print("A total of", c, "characters have been removed from your string.")
      print("The new string is:", s)
      

      相当容易阅读. Python高度重视可读性,因为使用可读代码可以更轻松地理解作者的意图.如果您的代码难以理解,那么您很有可能还有一些重构需要完成!

      is rather easier to read. Python values readability so highly because with readable code it's easier to understand the intent of the author. If your code is hard to understand there's a good chance you still have some refactoring to do!

      这篇关于清理没有拆分/剥离/内置功能的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆