带有列表和集合的Python函数

时间:2022-02-23 18:06:50

So I'm trying to figure out this problem and I can't figure out why it isn't working.

所以我试图弄清楚这个问题,我无法弄清楚它为什么不起作用。

The premise is that you're given an input list and you have to find the second-lowest value. The list can have any number of integers and can repeat values; you can't change the list.

前提是你给了一个输入列表,你必须找到第二低的值。列表可以包含任意数量的整数,并且可以重复值;你不能改变名单。

My code:

我的代码:

def second_min(x):
    input_list = list(x)
    print input_list
    list_copy = list(input_list)
    list_set = set(list_copy)
    if len(list_set) > 1:
        list_copy2 = list(list_set)
        list_copy2 = list_copy2.sort()
        return list_copy2[1]
    else:
        return None

print second_min([4,3,1,5,1])
print second_min([1,1,1])

The outputs for those two inputs are:

这两个输入的输出是:

3
None

It's giving me errors on lines 9 and 13.

这给了我第9和第13行的错误。

TypeError: 'NoneType' object has no attribute '__getitem__'

Thanks!

谢谢!

3 个解决方案

#1


5  

list_copy2 = list_copy2.sort()

.sort() sorts the list in place and returns None. So you're sorting the list, then throwing it away. You want just:

.sort()对列表进行排序并返回None。所以你要对列表进行排序,然后将其丢弃。你想要的只是:

list_copy2.sort()

Or:

要么:

list_copy2 = sorted(list_set)

sorted always returns a list, so you can use it to sort the set and convert it to a list in one step!

sorted总是返回一个列表,因此您可以使用它来对集合进行排序并一步将其转换为列表!

#2


2  

You need to use sorted instead of sort. sorted returns a new list, that is a sorted version of the original. sort will sort the list in-place, and returns None upon doing so.

您需要使用sorted而不是sort。 sorted返回一个新列表,即原始的排序版本。 sort将对列表进行就地排序,并在执行此操作时返回None。

def second_min(x):
    if len(x) > 1:
        return sorted(x)[1]
    else:
        return None

>>> second_min([4,3,1,5,1])
1

#3


0  

Help, I can't use sorted! It's not allowed!

帮忙,我不能用排序!这不被允许!

def second_min(li):
    if len(li) < 2:
        return None
    it = iter(li)
    a, b = next(it), next(it)
    next_lowest, lowest = max(a, b), min(a, b)
    for x in it:
        if x < next_lowest:
            if x < lowest:
                lowest, next_lowest = x, lowest
            else:
                next_lowest = x
    return next_lowest

#1


5  

list_copy2 = list_copy2.sort()

.sort() sorts the list in place and returns None. So you're sorting the list, then throwing it away. You want just:

.sort()对列表进行排序并返回None。所以你要对列表进行排序,然后将其丢弃。你想要的只是:

list_copy2.sort()

Or:

要么:

list_copy2 = sorted(list_set)

sorted always returns a list, so you can use it to sort the set and convert it to a list in one step!

sorted总是返回一个列表,因此您可以使用它来对集合进行排序并一步将其转换为列表!

#2


2  

You need to use sorted instead of sort. sorted returns a new list, that is a sorted version of the original. sort will sort the list in-place, and returns None upon doing so.

您需要使用sorted而不是sort。 sorted返回一个新列表,即原始的排序版本。 sort将对列表进行就地排序,并在执行此操作时返回None。

def second_min(x):
    if len(x) > 1:
        return sorted(x)[1]
    else:
        return None

>>> second_min([4,3,1,5,1])
1

#3


0  

Help, I can't use sorted! It's not allowed!

帮忙,我不能用排序!这不被允许!

def second_min(li):
    if len(li) < 2:
        return None
    it = iter(li)
    a, b = next(it), next(it)
    next_lowest, lowest = max(a, b), min(a, b)
    for x in it:
        if x < next_lowest:
            if x < lowest:
                lowest, next_lowest = x, lowest
            else:
                next_lowest = x
    return next_lowest