在数组中查找第二大项目无法正常工作

时间:2019-02-19 02:07:27

标签: python arrays max

我的程序找到第二个最小的数字可以正常工作,但是运行代码查找第二个最大的数字时,第二个最大的输出是不正确的。我以类似的方式完成了第二个功能,但是将大于号翻转了过来。修复代码的最佳方法是什么?

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)

5 个答案:

答案 0 :(得分:2)

分配新的最高编号时,需要将第二高的编号分配给旧的最高编号。

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()

答案 1 :(得分:1)

您没有将变量设置为包含最大值。如果您创建一个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开始迭代会更好,因为已经分配了第一个和第二个。

答案 2 :(得分:1)

在顶部声明

    num_list = [1,5,6,44,5,7,2,444,3]
    smallest_num = num_list[0]
    second_smallest_num = num_list[1]

您是否意识到您正在访问列表中的第零个元素和第一个元素

smallest_num = 1
second_smallest_num = 5

顺便说一句,第零个元素(即= 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)

答案 3 :(得分:0)

为什么不使用heapq?

>>> import heapq
>>> heapq.nlargest(2, num)
[10, 8]
>>> heapq.nsmallest(2, num)
[1, 2]
>>>

有关https://docs.python.org/2/library/heapq.html的更多信息

答案 4 :(得分:0)

这里最简单的方法是简单地使用python内置AVERROR_NOMEMmax()。请注意,我们正在复制传入列表,以使原始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,然后从副本中删除它们,然后从其余部分中取出minmax

请注意,我已经从函数中提取了min,但是将它们保留在函数中是可行的。这种方法使您可以在代码中的不同列表中重复使用这些功能。

或者,如此处另一个答案所述,您可以从@andercruzbr导入num_list模块,只需执行以下操作:

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])