简体   繁体   English

在列表中查找大于给定值的最小元素

[英]Finding the smallest element greater than a given value in a list

I am writing a function that returns the minimum value of numbers that are greater than a certain value in a list. 我正在编写一个函数,该函数返回大于列表中某个值的数字的最小值。 For instance, if the given value is 3 from [1,2,3,4,5], it should return 4. However, none of my attempts are working. 例如,如果给定的值是[1,2,3,4,5]中的3,则应返回4。但是,我的任何尝试均无效。

I have used the 'min' function, and tried the while and for loop to solve the problem. 我使用了'min'函数,并尝试了while和for循环来解决问题。

def smallest_greater(seq, value):
    i = 0
    while i < len(seq):
        if seq[i] > value:
            i = i + 1
    return min(seq[i])

def smallest_greater(seq, value):
    i = 0
    for value in seq:
        if seq[i] > value:
            i = i + 1
    return min(seq[i])

If I try running the code with the while loop, it does not execute the code. 如果我尝试使用while循环运行代码,它将无法执行代码。 If I run the code with for loop, it gives "TypeError: 'int' object is not iterable" 如果我使用for循环运行代码,则会显示“ TypeError:'int'对象不可迭代”

Your while loop will go infinite loop unless every number in seq is greater than value (which is what's happening, since i will never exceed len(seq) ) 您的while循环将变为无限循环,除非seq每个数字都大于value (这是发生的情况,因为i永远不会超过len(seq)

Besides, incrementing i is doing a count of number greater than value , and not exactly giving the index of smallest greater. 此外,递增i对大于value的数字进行计数 ,而不是恰好给出大于最小的索引。

You can do this in one line with list comprehension: 您可以使用列表理解功能在一行中完成此操作:

min(i for i in [1,2,3,4,5] if i > 3)

Output: 输出:

4

Proven that you are given an unordered list of numbers, then you would need to sort the list first. 事实证明,您将获得无序的数字列表,那么您需要首先对列表进行排序。

on your code, the first smallest_greater should work if you switch the > with <= in if seq[i] > value: . 在代码上,如果在if seq[i] > value:中用<=切换> ,则第一个smallest_greater应该可以工作。 However, the return value shouldn't be min(seq[i]) but instead only seq[i] . 但是,返回值不应为min(seq[i]) ,而应仅为seq[i] For the other one, the problem is that you are overwriting the value of value with the for loop leading to your code's failure. 对于另一个问题,问题是您用for loop覆盖了value的value ,导致代码失败。

Fixing your first function, you will have a working code with: 修复您的第一个功能后,您将获得一个具有以下功能的代码:

def smallest_greater(seq, value):
    seq.sort()
    i = 0
    while i < len(seq):
        if seq[i] <= value:
            i = i + 1
        else: return seq[i]

Which should give your desired output. 哪个应该给出您想要的输出。

Edit: 编辑:

If I were to do this, then it will be: 如果我要这样做,那么它将是:

def smallest_greater(seq, value):
    try:
        return min(x for x in seq if x > value)
    except ValueError:
        return None

Your code is taking minimum of one value rather it should take minimum of the filtered list. 您的代码至少要取一个值,而应该取最小的过滤列表。 First populate another list with the desired value that satisfies the criteria and then take the minimum: 首先用满足条件的期望值填充另一个列表,然后取最小值:

In [3]: def smallest_greater(seq, value):
   ...:     lst = []
   ...:     for s in seq:
   ...:         if s > value:
   ...:             lst.append(s)
   ...:     return min(lst)


In [4]: smallest_greater([1, 2, 3, 4, 5, 6], 5)
Out[4]: 6

In [5]: smallest_greater([1, 2, 3, 4, 5, 6], 3)
Out[5]: 4
seq.sort()
i = seq.index(value)
if (i < seq.size() - 1):
    return seq[i+1]
else return seq[i]

Sort array, find index of the value, the next value in list will be the least greater than your value. 对数组进行排序,找到值的索引,列表中的下一个值将比您的值最小。 If statement checks if your value is the greatest in array if语句检查您的值是否在数组中最大

sorted and min all uses extra looping. 排序和最小都使用额外的循环。 You could do it in a single loop for better performance 您可以在单个循环中完成操作以获得更好的性能

k = 3
x = None
for item in a:
    x = item if item > k and (x is None or x > item) else x

Now you have the smallest greater element in x if there is any else None 现在,如果有其他元素,则x中具有最小的更大元素

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

相关问题 在排序列表中查找大于给定数字的最小数字 - Find the smallest number that is greater than a given number in a sorted list 查找列表的最小元素 - Finding smallest element of list 查找列表中的值是否大于其下方的项目 - Finding if a value in a list is greater than the item below it 查找大于某个值的嵌套列表的索引 - Finding index of nested list greater than a value python:当最小元素数大于1时列表中的第二个最小值 - python: second smallest value in list when number of smallest elements is greater than 1 Bigtable python 客户端:如何检索大于给定值的最小行键 - Bigtable python Client: How do I retrieve the smallest rowkey that is greater than a given value 查找列表中大于当前元素的所有元素 - Finding all elements greater than current element in a list 从排序列表中获取大于给定数字的第一个元素 - Get first element greater than a given number from a sorted list 搜索大于给定值的值的字典列表? - Search list of dicts for values greater than given value? Numpy:在给定索引数组的情况下找到具有最小值的数组中元素索引的有效方法 - Numpy: Efficient way of finding the index of the element in an array with the smallest value given an index array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM