我在查看list.pop()时遇到一些奇怪的行为。观察:
#Let's use this list for our experiment
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
如果我们在for循环中使用range来打印和弹出列表中的元素,则:
for x in range(1, len(lst) + 1):
print(x, '*' * len(lst), len(lst))
lst.pop()
#We get, as we would expect:
1 ********** 10
2 ********* 9
3 ******** 8
4 ******* 7
5 ****** 6
6 ***** 5
7 **** 4
8 *** 3
9 ** 2
10 * 1
现在,如果不使用range(),我们将用于列表中的n 会观察到不同的行为:
#Create a function to make it easier
def print_and_pop(num_list):
for n in num_list:
print(n, '*' * len(num_list), len(num_list))
num_list.pop()
#1st Run
print_and_pop(lst)
1 ********** 10
2 ********* 9
3 ******** 8
4 ******* 7
5 ****** 6
如果再次运行相同的代码,则会得到:
print_and_pop(lst)
#2nd Run
1 ***** 5
2 **** 4
3 *** 3
print_and_pop()
#3rd Run
1 ** 2
#4th Run
1 * 1
现在列表为空。使用更长的列表将使用更多的运行。一个包含20个元素的列表,有6个运行,而有50个元素7。基本上,每次都会弹出一半的元素。
答案 0 :(得分:0)
这种行为确实是出乎意料的。您正在为列表创建一个迭代器,在每个循环中有效地在迭代器上调用next。在每次迭代过程中从列表中弹出元素时,迭代器在找不到列表中的下一个元素时将引发StopIteration异常。因此,当您从头开始弹出元素时,它只能循环math.ceil(len(lst) / 2.0)
元素,并且列表现在包含尚未弹出的项目。通过其他函数调用重复此操作会导致相同的行为