简体   繁体   English

如何在列表中找到第二个最小的UNIQUE编号?

[英]How to find second smallest UNIQUE number in a list?

I need to create a function that returns the second smallest unique number, which means if 我需要创建一个函数,返回第二个最小的唯一数字,这意味着如果
list1 = [5,4,3,2,2,1] , I need to return 3, because 2 is not unique. list1 = [5,4,3,2,2,1] ,我需要返回3,因为2不是唯一的。

I've tried: 我试过了:

def second(list1):
    result = sorted(list1)[1]
    return result

and

def second(list1):
    result = list(set((list1)))
    return result

but they all return 2. 但他们都返回2。

EDIT1: EDIT1:

Thanks guys! 多谢你们! I got it working using this final code: 我使用这个最终代码工作了:

def second(list1):
    b = [i for i in list1 if list1.count(i) == 1]
    b.sort()
    result = sorted(b)[1]
    return result

EDIT 2: 编辑2:

Okay guys... really confused. 好吧......真的很困惑。 My Prof just told me that if list1 = [1,1,2,3,4] , it should return 2 because 2 is still the second smallest number, and if list1 = [1,2,2,3,4] , it should return 3. Code in eidt1 wont work if list1 = [1,1,2,3,4] . 我的教授告诉我,如果list1 = [1,1,2,3,4] ,它应该返回2,因为2仍然是第二个最小的数字,如果list1 = [1,2,2,3,4] ,它应该返回3.如果list1 = [1,1,2,3,4] ,eidt1中的代码将不起作用。 I think I need to do something like: 我想我需要做的事情如下:

if duplicate number in position list1[0], then remove all duplicates and return second number. 如果位置list1 [0]中有重复的数字,则删除所有重复项并返回第二个数字。 Else if duplicate number postion not in list1[0], then just use the code in EDIT1. 否则,如果重复的数字位置不在list1 [0]中,那么只需使用EDIT1中的代码即可。

Without using anything fancy, why not just get a list of uniques, sort it, and get the second list item? 没有使用任何花哨的东西,为什么不只是得到一个独特的列表,排序它,并得到第二个列表项?

a = [5,4,3,2,2,1] #second smallest is 3
b = [i for i in a if a.count(i) == 1]
b.sort()
>>>b[1]
3


a = [5,4,4,3,3,2,2,1] #second smallest is 5
b = [i for i in a if a.count(i) == 1]
b.sort()
>>> b[1]
5

Obviously you should test that your list has at least two unique numbers in it. 显然你应该测试你的列表中至少有两个唯一的数字。 In other words, make sure b has a length of at least 2. 换句话说,确保b的长度至少为2。

  1. Remove non unique elements - use sort / itertools.groupby or collections.Counter 删除非唯一元素 - 使用sort / itertools.groupbycollections.Counter
  2. Use min - O(n) to determine the minimum instead of sort - O(nlongn). 使用min - O(n)来确定最小值而不是 sort - O(nlongn)。 (In any case if you are using groupby the data is already sorted) I missed the fact that OP wanted the second minimum, so sorting is still a better option here (无论如何,如果你使用 groupby数据已经排序了) 我错过了OP想要第二个最小值的事实,所以这里排序仍然是一个更好的选择

Sample Code 示例代码

Using Counter 使用计数器

>>> sorted(k for k, v in Counter(list1).items() if v == 1)[1]
1

Using Itertools 使用Itertools

>>> sorted(k for k, g in groupby(sorted(list1)) if len(list(g)) == 1)[1]
3

Okay, here usage of set() on a list is not going to help. 好的,这里使用列表中的set()不会有帮助。 It doesn't purge the duplicated elements. 它不会清除重复的元素。 What I mean is : 我的意思是 :

l1=[5,4,3,2,2,1]
print set(l1)

Prints 打印

[0, 1, 2, 3, 4, 5]

Here, you're not removing the duplicated elements, but the list gets unique 在这里,您不是删除重复的元素,但列表是唯一的

In your example you want to remove all duplicated elements. 在您的示例中,您要删除所有重复的元素。 Try something like this. 尝试这样的事情。

l1=[5,4,3,2,2,1]
newlist=[]
for i in l1:
    if l1.count(i)==1:
    newlist.append(i)
print newlist 

This in this example prints 在此示例中打印

[5, 4, 3, 1]

then you can use heapq to get your second largest number in your list, like this 然后你可以使用heapq来获得列表中的第二大数字,就像这样

print heapq.nsmallest(2, newlist)[-1]

Imports : import heapq , The above snippet prints 3 for you. 导入: import heapq ,上面的代码段为您打印3 This should to the trick. 这应该是诀窍。 Cheers! 干杯!

Here's a fancier approach that doesn't use count (which means it should have significantly better performance on large datasets). 这是一种不使用count的更好的方法(这意味着它应该在大型数据集上具有明显更好的性能)。

from collections import defaultdict

def getUnique(data):
    dd = defaultdict(lambda: 0)
    for value in data:
        dd[value] += 1
    result = [key for key in dd.keys() if dd[key] == 1]
    result.sort()
    return result

a = [5,4,3,2,2,1]
b = getUnique(a)
print(b)
# [1, 3, 4, 5]
print(b[1])
# 3

Okay guys! 好的伙计们! I got the working code thanks to all your help and helping me to think on the right track. 我得到了你的所有帮助,并帮助我思考正确的轨道,我得到了工作代码。 This code works: 此代码有效:

`def second(list1):
    if len(list1)!= len(set(list1)):
         result = sorted(list1)[2]
        return result
    elif len(list1) == len(set(list1)):
        result = sorted(list1)[1]
        return result`

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

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