【问题标题】:Return empty string as True in given string在给定的字符串中将空字符串返回为 True
【发布时间】:2020-02-10 08:04:17
【问题描述】:
def first_and_last(message):
        if (message[0] == message[3]):
            return True
        elif (message[0] != message[3]):
            return False


 print(first_and_last("else"))
 print(first_and_last("tree"))
 print(first_and_last(""))

如果字符串为空,我想返回True,如果字符串的第一个和最后一个字母匹配,则返回True,如果不匹配,则返回False,但是我已经编写了关于2个场景的代码,但我不知道如何获取空字符串作为真正的帮助。

【问题讨论】:

  • if (message == ''): return True 是一个选项。你怎么看?
  • 您要比较最后一个字符还是第四个字符?如果是前者,则应使用[-1] 而不是[3]。如果您使用[3],那么first_and_last("abcad") 将返回True

标签: python python-3.x string


【解决方案1】:
  • 你写过你想比较第一个字符和最后一个字符, 所以你必须使用[-1] 而不是[3]。否则你在比较 第一个和第四个字符。

  • 可以使用if not message判断是否为空字符串

  • 既然是返回,就不用检查是否不匹配了。


def first_and_last(message):
    if not message:
        return True
    return message[0] == message[-1]

正如 cmets 中所述,这可以更多地推入一行:

def first_and_last(message):
    return not message or message[0] == message[-1]

【讨论】:

  • 为什么不直接使用:return not message or message[0] == message[-1]?这也返回布尔值!
  • @AjitZero 完全正确(我会添加它),我最初觉得它有点过分了
  • @DeepSpace 如果你能帮忙stackoverflow.com/questions/60139211/…
【解决方案2】:

您可以使用if not

def first_and_last(message):
if not message:
    return False
else: return True


print(first_and_last("else")) #True
print(first_and_last("tree")) #True
print(first_and_last("")) #False

【讨论】:

    【解决方案3】:
    def first_and_last(message):
        if not message:
            return True
        elif (message[0] == message[-1]):
            return True
        elif (message[0] != message[-1]):
            return False
    
    
    print(first_and_last("else"))
    print(first_and_last("tree"))
    print(first_and_last(""))
    

    【讨论】:

    • 您好,欢迎来到堆栈溢出。请edit您的答案解释一下这个答案如何改进其他答案?
    【解决方案4】:

    只是为了使其更具可读性,因为您正在学习并且没有使用“如果不是”

    def first_and_last(message):
        if message == "":
            return True
        elif message[0] == message[-1]:
            return True  
        else:
            return False
    
    print(first_and_last("else"))
    print(first_and_last("tree"))
    print(first_and_last(""))
    
    output would be  below 
    
    True
    False
    True
    

    【讨论】:

      【解决方案5】:
      # defines, name and establishes the arguments for the function
      def first_and_last(message):
       
          # "if not message" checks if the parameter is empty
          # message[0] gets the first character in the parameter
          # message[-1] gets the last character in the parameter
          # "==" checks if the first and last character are the same in the 
          # parameter
          # the "or" comparator checks if the statements on either side or 
          # the comparator is true 
      
          if not message or message[0] == message[-1]:
              # if either one or both of the above statements are true the 
              # function returns True
      
              return True
          # otherwise returns False
      
          return False
      

      【讨论】:

      • 请记住,Stack Overflow 不仅仅是为了解决眼前的问题,而是为了帮助未来的读者找到类似问题的解决方案,这需要了解底层代码。这对于我们社区的初学者和不熟悉语法的成员来说尤其重要。鉴于此,您能否edit 您的答案包括对您正在做什么的解释以及为什么您认为这是最好的方法?
      【解决方案6】:
      def first_and_last(message):
          if message == "" or message[0] == message[-1]:
              return True
          else:
              return False
      
      print(first_and_last("else"))
      print(first_and_last("tree"))`enter code here`
      print(first_and_last(""))
      

      【讨论】:

        【解决方案7】:

        您可以使用if not message: 来检查消息变量是否为空。

        例如,您可以执行以下操作:

        def first_and_last(message):
            if not message:
                return True
            elif (message[0] == message[3]):
                    return True
            elif (message[0] != message[3]):
                    return False
        
        
        print(first_and_last("else"))
        print(first_and_last("tree"))
        print(first_and_last(""))
        

        【讨论】:

        • 不要硬编码[3],因为OP想要第一个和最后一个字符,使用[-1]
        • @DeepSpace 我没有硬编码 [3]。我收回了作者提供的用于解决问题的代码。我认为没有必要评论答案中的所有帖子以注意到同一件事。但是,您可以完全通知帖子作者,[-1] 调用返回了 python list/str... 中的最后一个字符。
        【解决方案8】:

        您可以使用bool(message) 检查字符串是否为空。并用message[-1]检查最后一项:

        def first_and_last(message):
            if not message:
                return True
            elif (message[0] == message[-1]):
                return True
            else:
                return False
        
        
        print(first_and_last("else")) # Returns True
        print(first_and_last("tree")) # Returns False
        print(first_and_last("")) # Returns True
        

        【讨论】:

        • if bool(...) 是多余的。只需if not message 就足够了
        • 另外,不要硬编码[3],因为OP需要第一个和最后一个字符,请使用[-1]
        【解决方案9】:

        用途:

        if not message:
        

        如果字符串为空,这将返回 true:

        def first_and_last(message):
                if not message:
                    return True
                if (message[0] == message[3]):
                    return True
                elif (message[0] != message[3]):
                    return False
        
        
        print(first_and_last("else"))
        print(first_and_last("tree"))
        print(first_and_last(""))
        

        【讨论】:

        • 不要硬编码[3],因为OP想要第一个和最后一个字符,使用[-1]
        【解决方案10】:
        def first_and_last(message):
        
        if message=="" or message[0]==message[-1]:
        
            return True
        
        return False
        

        这里,message == "" 检查空字符串,在“或”之后检查单词的第一个和最后一个字母。

        【讨论】:

          【解决方案11】:
          # could it work like this as well? if not why? thanks :)
          def first_and_last(message):
          while bool(message) == True:
              first_letter = str(message[0])
              last_letter = str(message[-1])
              if first_letter == last_letter:
                  return True
              else :
                  return False
           return True
          
           print(first_and_last("else"))
           print(first_and_last("tree"))
           print(first_and_last(""))
           # Output (True, False, True)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-08-10
            • 1970-01-01
            • 2015-02-20
            • 1970-01-01
            • 2018-11-07
            • 2020-09-05
            • 2021-06-23
            相关资源
            最近更新 更多