我在试着理解python中的一些代码。(我不懂python,只懂c/c++)
def merge(left, right):
result = []
left_idx, right_idx = 0, 0
while left_idx < len(left) and right_idx < len(right):
if left[left_idx] <= right[right_idx]:
result.append(left[left_idx])
left_idx += 1
else:
result.append(right[right_idx])
right_idx += 1
if left: # Confused by this line.
result.extend(left[left_idx:])
if right:
result.extend(right[right_idx:])
return result
我想除了if left
和if right
语句之外,我已经理解了上面的大部分代码。我理解if语句的方式是,它们后面必须有一个计算结果为1或0的表达式。你知道吗
像这样想,if之后给出的任何条件都将被计算,然后强制为布尔值(不是0和1)。If True->;执行If的主体。你知道吗
相关问题 更多 >
编程相关推荐