简体   繁体   English

如何在 python 的列表中找到第二小的数字

[英]How to find the second lowest number in a list in python

I wrote this code to find the second lowest number in a list, but I'm not getting the correct result.我编写这段代码是为了找到列表中第二小的数字,但我没有得到正确的结果。 Can you show me what I did wrong?你能告诉我我做错了什么吗?

def second_lowest_number(list):
    
    the_minimum = min(list)
    new = []
    for i in range(len(t)):
        if list[i] > the_minimum:
            new.append(list[i])

    return min(new)
print(sorted(the_list)[1])

I would think would work and would not be any slower than any other solution我认为会起作用并且不会比任何其他解决方案慢

generally just keep it simple通常只是保持简单

if it has duplicates just use a set如果它有重复项,只需使用一组

 print(sorted(set(the_list))[1]) 

note that the first solution will fail if there is only one item in the list, and the second solution will fail if all items in the list are the same value请注意,如果列表中只有一个项目,第一个解决方案将失败,如果列表中的所有项目都具有相同的值,则第二个解决方案将失败

One problem with your function is that there is no variable t so the for loop would not run. function 的一个问题是没有变量 t,因此 for 循环不会运行。 Also, "list" is an in-built python keyword so avoid using that as the parameter name.此外,“list”是一个内置的 python 关键字,因此请避免将其用作参数名称。 Here is the working version of your implementation:这是您的实施的工作版本:

def seconde_lowest_number(arr):
    
    the_minimum = min(arr)
    new = []
    for i in range(len(arr)):
        if arr[i] > the_minimum:
            new.append(arr[i])

    return min(new)

although I would recommend just sorting the list and returning the 2nd index like so:虽然我建议只对列表进行排序并返回第二个索引,如下所示:

def second_lowest_number(arr):
    arr.sort()
    return arr[1]

Actually, you have a typo in your for loop.实际上,您的 for 循环中有错字。
It needs to be like this它需要像这样

for i in range(len(list)):

But, I would like to say something to you.但是,我想对你说几句话。
1. Don't use built-in function name as your variable/list name. 1.不要使用内置的 function 名称作为变量/列表名称。 You can use newlist instead.您可以newlist

2 . 2 . It's better to avoid using range(len(listname)) .最好避免使用range(len(listname))
You can try this instead你可以试试这个

for i in list:

The answer depends on what your input list looks like, if your list contains unique numbers (no repetition) you can do something like below答案取决于您的输入列表的样子,如果您的列表包含唯一数字(无重复),您可以执行如下操作

numbers = [3, 6, 1, 7, 8, 2]


def second_lowest_number(arr):
    arr.sort()
    return arr[1]

However, if your list contains duplicated numbers, you could try something like this但是,如果您的列表包含重复的数字,您可以尝试这样的事情

numbers = [3, 3, 1, 1, 5, 6]


def second_lowest_number(arr):
    arr.sort()
    for i in arr:
        if i > min(arr):
            return i

If you need it in linear time, I think this might work:如果您在线性时间内需要它,我认为这可能有效:

def second_lowest_number(list):
    the_minimum = list[0]
    second_min = the_minimum
    flag = False
    for i in range(len(list)):
       if list[i] < the_minimum:
           second_min = the_minimum
           the_minimum = list[i]
       elif the_minimum == second_min and list[i] > the_minimum and flag == False:
           second_min = list[i]
           flag = True
       elif list[i] < second_min and list[i] != the_minimum:
           second_min = list[i]
    return second_min
print(second_lowest_number([-2,3,5,2,2,0,5,1,2,3,5,-1]))

Please don't use this as a final answer, I'm just trying to brainstorm and aid as best as I can.请不要将此作为最终答案,我只是想集思广益并尽我所能提供帮助。 If someone found cases where this code fails please share with me.如果有人发现此代码失败的情况,请与我分享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM