将`if(condition): return True`简化为一行返回语句会改变Python中的结果

huangapple go评论71阅读模式
英文:

Simplifying if(condition): return True to one line return statement changes results in Python

问题

I do not understand why the two methods written below perform differently. Any insights into why the simplified statement in xyz_there() fails for this test case would be appreciated!

str = "abcxyz"

def xyz_there(str):
  for i in range(len(str)):
    return (str[i:i+3] == 'xyz' and str[i-1] != '.')
  return False

def xyz_there2(str):
  for i in range(len(str)):
    if str[i:i+3] == 'xyz' and str[i-1] != '.':
      return True
  return False

print(xyz_there(str))  
print(xyz_there2(str))

It has always been my understanding that the return(condition) was the most efficient way to write that expression. I am aware that functions like find() and count() can also be used to solve this problem.

英文:

I do not understand why the two methods written below perform differently. Any insights into why the simplified statement in xyz_there() fails for this test case would be appreciated!

str = "abcxyz"

def xyz_there(str):
  for i in range(len(str)):
    return (str[i:i+3] == 'xyz' and str[i-1] != '.')
  return False

def xyz_there2(str):
  for i in range(len(str)):
    if str[i:i+3] == 'xyz' and str[i-1] != '.':
      return True
  return False

print(xyz_there(str))  
print(xyz_there2(str))

It has always been my understanding that the return(condition) was the most efficient way to write that expression. I am aware that functions like find() and count() can also be used to solve this problem.

答案1

得分: 1

欢迎来到stackoverflow! 将`if(condition): return True`简化为一行返回语句会改变Python中的结果

这两个函数之间的区别在于,在第二个函数xyz_there2中,return True语句位于if语句内部,因此只有在条件为真时才执行。如果条件不成立,循环会继续到下一个i的值。

然而,在第一个函数xyz_there中,return语句位于任何if语句内部,因此它在循环的第一次迭代中执行,从而返回False值。之后函数退出,循环没有机会迭代到下一个i的值。

换句话说,xyz_there函数在第一次循环迭代时返回False,当i==0时。而xyz_there2会迭代直到i==3,然后返回True

英文:

Welcome to stackoverflow! 将`if(condition): return True`简化为一行返回语句会改变Python中的结果

Difference between these two functions is that in second function, xyz_there2, the return True statement is inside if, so it is executed only if the condition is true. If it is not true - loop continues to next value of i.

However, in first function xyz_there, the return statement is not inside of any if statement, so it's executed on first iteration of the loop, thus returning False value. And after that the function exits, and loop doesn't get a chance to iterate to next value of i.

In other words, xyz_there function returns False at first loop iteration, when i==0. While xyz_there2 iterates until i==3, and returns True.

答案2

得分: 0

你的第一个函数等同于以下代码,它返回 False:

def xyz_there(str):
  return (str[0:3] == 'xyz' and str[-1] != '.')

因为你没有遍历字符串中的所有字符,而只是对单词的前三个字母做出决定并返回结果。

英文:

your first function is equivalent to this which is returning Flase :

def xyz_there(str):
  return (str[0:3] == 'xyz' and str[-1] != '.')

because you are not looping through all the characters in the string , you are just making decision on the first 3 letters of the word and return it

huangapple
  • 本文由 发表于 2023年7月20日 21:53:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定