简体   繁体   English

Python 中的 IF 条件是如何工作的?

[英]How does this IF-Conditions in Python work?

This is a Mergesort i found online and tried to implement.这是我在网上找到并尝试实施的 Mergesort。 It worked but i am not sure how it works.它有效,但我不确定它是如何工作的。 Can someone please explain to me why/how this if-condition works?有人可以向我解释这个 if 条件为什么/如何工作吗? Where does the code continue when the first-if statement isnt true anymore.当 first-if 语句不再为真时,代码从哪里继续。 I know how if-condtions work in general but to the lack of indentions and/or 'else' confuses me here.我知道 if-condtions 通常是如何工作的,但是由于缺乏缩进和/或“其他”让我在这里感到困惑。

def mergeSort(alist):
   print("Splitting ",alist)
   if len(alist)>1:
       mid = len(alist)//2
       lefthalf = alist[:mid]
       righthalf = alist[mid:]
       #recursion
       mergeSort(lefthalf)
       mergeSort(righthalf)

       i=0
       j=0
       k=0

       while i < len(lefthalf) and j < len(righthalf):
           if lefthalf[i] < righthalf[j]:
               alist[k]=lefthalf[i]
               i=i+1
           else:
               alist[k]=righthalf[j]
               j=j+1
           k=k+1

       while i < len(lefthalf):
           alist[k]=lefthalf[i]
           i=i+1
           k=k+1

       while j < len(righthalf):
           alist[k]=righthalf[j]
           j=j+1
           k=k+1

alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)```

This relies on alist effectively acting like a global variable, which tends to be a bit frowned upon (especially since it's not declared global).这依赖于alist有效地充当全局变量,这往往有点不受欢迎(特别是因为它没有声明为全局变量)。 There are also some while loops that could be replaced by list comprehensions.还有一些 while 循环可以用列表理解代替。

If the if condition is not true, then alist has only one element, and so sorting it is trivial: a sorted list of one element is just that list.如果if条件不为真,则alist只有一个元素,因此对其进行排序很简单:一个元素的排序列表就是该列表。 A list of only element can't be out of order.只有元素的列表不能乱序。 So the function simply terminates without doing anything, because nothing is needed.所以这个函数什么都不做就直接终止了,因为不需要任何东西。

If the if condition is true, then function reassigns entries in the list, using recursive calls to itself.如果if条件为真,则函数使用对自身的递归调用重新分配列表中的条目。 Each recursion calls the function on a smaller list, so eventually a list of length one is reached, terminating the recursion.每个递归调用较小列表上的函数,因此最终到达长度为 1 的列表,终止递归。 So when the if condition is false, the function just returns to the next higher level of recursion.所以当if条件为假时,函数只是返回到下一个更高层次的递归。

Because of the way mutable data types such as lists work in Python, when a recursion level changes what's in a particular location of the alist that it was passed, it also alters what's in that location in the alist of the higher recursion levels.由于可变数据类型(例如列表)在 Python 中的工作方式,当递归级别更改alist传递的列表的特定位置中的内容时,它也会更改更高alist级别列表中该位置中的内容。 This results in the original alist ending up sorted, even though the function doesn't have the return keyword and thus doesn't explicitly return anything.这导致原始的alist最终排序,即使该函数没有return关键字,因此没有显式返回任何东西。 This is known as a function operating on the input "in place".这被称为对输入“就地”进行操作的函数。

In this code there is no else used - to find the matching else always look for else that starts at the same position(with same indentation) as if.在此代码中没有使用 else - 要找到匹配的 else,总是寻找从相同位置(具有相同缩进)开始的 else,就好像。 Since there is no such code, it will exit the function when if condition is not satisfied.由于没有这段代码,所以当if条件不满足时就会退出函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM