我找到第二个最小数字的程序按预期工作,但是当运行代码找到第二大数字时,第二个最大输出不正确。我以类似的方式完成了第二个功能,但翻转了大于号。修复代码的最佳方法是什么?
def second_smallest():
num_list = [1,5,6,44,5,7,2,444,3]
smallest_num = num_list[0]
second_smallest_num = num_list[1]
for i in num_list:
if i < smallest_num:
smallest_num = i
elif smallest_num < i < second_smallest_num:
second_smallest_num = i
print("Smallest Number:", smallest_num)
print("Second smallest number:", second_smallest_num)
def second_largest():
num_list = [1,5,6,44,5,7,2,444,3]
largest_num = num_list[0]
second_largest_num = num_list[1]
for i in num_list:
if i > largest_num:
largest_num = i
elif second_largest_num < i < largest_num:
second_largest_num = i
print("Largest:", largest_num)
print("Second largest:", second_largest_num)
分配新的最高编号时,需要将第二个最高编号分配给旧的最高编号。
def second_smallest():
num_list = [1,5,6,44,5,7,2,444,3]
smallest_num = num_list[0]
second_smallest_num = num_list[1]
for i in num_list:
if i < smallest_num:
smallest_num = i
elif smallest_num < i < second_smallest_num:
second_smallest_num = i
print("Smallest Number:", smallest_num)
print("Second smallest number:", second_smallest_num)
def second_largest():
num_list = [1,5,6,44,5,7,2,444,3]
largest_num = num_list[0]
second_largest_num = num_list[1]
for i in num_list:
if i > largest_num:
second_largest_num = largest_num
largest_num = i
elif second_largest_num < i < largest_num:
second_largest_num = i
print("Largest:", largest_num)
print("Second largest:", second_largest_num)
second_smallest()
second_largest()
您没有设置变量来保持最大值。如果您创建一个tmp
变量来保存最大值并将其分配给第二大值,它将起作用
def second_largest():
num_list = [1,5,6,44,5,7,2,444,3]
largest_num = num_list[0]
second_largest_num = num_list[1]
for i in num_list:
if i > largest_num:
tmp = largest_num
largest_num = i
second_largest_num = tmp
elif second_largest_num < i < largest_num:
second_largest_num = i
print("Largest:", largest_num)
print("Second largest:", second_largest_num)
此外,如果从数组的索引2开始迭代作为第一个和第二个已经分配将更好。
在顶部,您声明的地方
num_list = [1,5,6,44,5,7,2,444,3]
smallest_num = num_list[0]
second_smallest_num = num_list[1]
你是否意识到你正在访问列表中的第0个和第一个元素,即
smallest_num = 1
second_smallest_num = 5
顺便说一句,第0个元素(即= 1)恰好是该列表中的最小元素,这就是为什么它适用于其中一个函数但不适用于另一个函数的原因。
应该做些什么
def second_largest():
num_list = [1,5,6,44,5,7,2,444,3]
largest_num = max(num_list)
### SOME FUNCTION THAT TAKES IN THAT LIST, REMOVES THE LARGEST NUM ELEMENT, AND THEN FINDS AND RETURNS THE NEXT LARGEST ELEMENT - I'll leave this to you ##
for i in num_list:
if i > largest_num:
largest_num = i
elif second_largest_num < i < largest_num:
second_largest_num = i
print("Largest:", largest_num)
print("Second largest:", second_largest_num)
为什么不使用heapq?
>>> import heapq
>>> heapq.nlargest(2, num)
[10, 8]
>>> heapq.nsmallest(2, num)
[1, 2]
>>>
这里最简单的方法就是使用python builtins max()
和min()
。请注意,我们正在制作传入列表的副本,以便原始的num_list
不会受到remove()
的影响:
num_list = [1,5,6,44,5,7,2,444,3]
def second_smallest(list):
temp_num_list = list.copy()
temp_num_list.remove(min(temp_num_list))
smallest_num = min(list)
second_smallest_num = min(temp_num_list)
return smallest_num, second_smallest_num
def second_largest(list):
temp_num_list = list.copy()
temp_num_list.remove(max(temp_num_list))
largest_num = max(list)
second_largest_num = max(temp_num_list)
return largest_num, second_largest_num
largest_num, second_largest_num = second_largest(num_list)
smallest_num, second_smallest_num = second_smallest(num_list)
print("Largest:", largest_num)
print("Second largest:", second_largest_num)
print("Smallest:", smallest_num)
print("Second smallest:", second_smallest_num)
输出:
Largest: 444
Second largest: 44
Smallest: 1
Second smallest: 2
没有必要进行任何比较。我们只是找到max
和min
,然后从副本中删除它们,然后从剩余部分中取出max
和min
。
请注意,我已从函数中抽象出num_list
,但将它们留在函数中会起作用。此方法允许您在代码中重用具有不同列表的函数。
或者,正如在另一个答案中所提到的,从@andercruzbr,你可以导入heapq
模块,并简单地做:
import heapq
num_list = [1,5,6,44,5,7,2,444,3]
def smallest_nums(list):
return heapq.nsmallest(2, num_list)
def largest_nums(list):
return heapq.nlargest(2, list)
print("Largest:", largest_nums(num_list)[0])
print("Second largest:", largest_nums(num_list)[1])
print("Smallest:", smallest_nums(num_list)[0])
print("Second smallest:", smallest_nums(num_list)[1])
输出:
Largest: 444
Second largest: 44
Smallest: 1
Second smallest: 2