【问题标题】:trying to figure out why same output but different results试图找出为什么相同的输出但不同的结果
【发布时间】:2021-01-03 19:12:16
【问题描述】:
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence

    # if old in sentence:
    #     i = len(sentence)
    #     N = len(old)
    #     wrong_sentence = sentence[0:i-N] + new
    #     return wrong_sentence

    if old in sentence == True:
        i = len(sentence)
        N = len(old)
        correct_sentence = sentence[0:i-N] + new
        return correct_sentence
    return sentence


print(replace_ending("She sells seashells by the seashore", "seashells", "donuts"))
# # Should display "She sells seashells by the seashore"

结果顶部:She sells seashells by thedonuts
结果底部:She sells seashells by the seashore

我一直试图弄清楚为什么两个“if 语句”都会产生“True”作为输出。但是,打印结果不同。有谁知道这是什么原因?

【问题讨论】:

  • 试试if sentence.endswith(old):
  • 抱歉,我弄糊涂了。比尔是对的,当被问及“如果句子中的旧 == True”即“False”时,底部的陈述结果为 false。因此,它只返回句子。

标签: python


【解决方案1】:

您的说法是所有这三个表达式都会返回相同的东西,这是不准确的。

Python 3.8.2 (default, Feb 26 2020, 02:56:10)
> ("a" in "aaa") == True
True
> "a" in "aaa" == True
False
> "a" in "aaa"
True

【讨论】:

    【解决方案2】:

    这是你所面临的声明

    print("seashells" in "She sells seashells by the seashore == True)
    False
    
    print("seashells" in "She sells seashells by the seashore)
    True
    
    

    在第一个:'"Seashells" in "She sells seashells by the seashores == True"',== 操作在in 操作之前执行,所以如果我们问"She sells seashells by the seashore == True" 结果是@ 987654325@ 那么如果我们继续询问"seashells" in False,结果将是False

    因此: "seashells" in "She sells seashells by the seashore == True""seashells" in "She sells seashells by the seashore == True"不一样

    而且,证据是:

    print(  ("seashells" in "She sells seashells by the seashore) == True)
    True
    

    所以在你的代码中,你必须使用:

     if (old in sentence) == True:
    

    得到您预期的响应。

    【讨论】:

      【解决方案3】:

      回答你原来的问题:

      我一直试图弄清楚为什么两个“if 语句”都会产生“True”作为输出。但是,打印结果不同。

      答案很简单,与以下有关:

      运算符优先级

      这样做:

      if old in sentence == True:
      

      Syntactic_sugar 这样做的吗:

      if old in sentence and sentence == True:
      

      所以正在发生的事情是分别检查这 2 个语句。

      因此,为了获得正确的优先级,(不使用 and)你应该这样做:

      if (old in sentence) == True:
      

      确实如此:

      1. inside old 中的任何字符是否也在句子中?

      2. 然后获取布尔值并询问结果是否等于True

      文档和指南

      请注意,比较、成员资格测试和身份测试都具有相同的优先级,并且具有从左到右的链接功能,如“比较”部分所述。

      最终考虑

      这不是您最初提出的问题,但是很自然地出现了您运行的函数似乎在逻辑上是错误的(从其中的 cmets、变量名称等判断)

      您运行 if 语句的方式是检查 Old 中 character 中的 any 是否在句子中,如果是,则将新词(甜甜圈)添加到它。所以这显然是真的。即使是一个字符也足以运行该语句,例如:

      print(replace_ending("She sells seashells by the seashore", "s", "donuts"))
      

      无论如何都会给你这个,因为 s 在句子中:

      解决方案

      使用anyall 函数以不同的方式描绘这一点(我留下了一个打印语句,以便您在列表l 中看到发生了什么)。

      你目前做的和这个是一样的

      def replace_ending(sentence, old, new):
      
          l = [char in old for char in sentence]
          print(l)
          if any(l):
              i = len(sentence)
              N = len(old)
              correct_sentence = sentence[0:i-N] + new
              return correct_sentence
          return sentence
      

      你想要(如果这是你想要实现的)使用 all 函数

      def replace_ending(sentance, old, new):
      
      
          N = len(old)
          get_last_word = sentance[-(N-1):] # -> Get seashore
          l = [char in old for char in get_last_word] 
          if all(l):
              i = len(sentance)
              correct_sentence = sentance[0:i-N] + new
              return correct_sentence
          return sentance
      

      意思是所有字符都应该在句子的最后一个单词中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        • 1970-01-01
        • 1970-01-01
        • 2018-01-14
        • 2012-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多