我正在学习python并学习google代码课程。在list2.py示例中,他们要求我们编写一个函数:Given two lists sorted in increasing order, create and return a merged
list of all the elements in sorted order. You may modify the passed in lists.
Ideally, the solution should work in "linear" time, making a single
pass of both lists.
他们给出了密码:def linear_merge(list1, list2):
result = []
#Look at the two lists so long as both are non-empty.
#Take whichever element [0] is smaller.
while len(list1) and len(list2):
if list1[0] < list2[0]:
result.append(list1.pop(0))
else:
result.append(list2.pop(0))
#Now tack on what's left
result.extend(list1)
result.extend(list2)
return result
我有个关于while循环的问题。我不确定我是否理解布尔测试是什么,以及它何时失败并中断循环。有谁能帮我更好地了解这件事吗?