简体   繁体   English

算法(Python):找到大于k的最小数

[英]Algorithm (Python): find the smallest number greater than k

I have a question from algorithm point of view. 我从算法的角度来看有一个问题。 I have a list of numbers (floats) 我有一个数字列表(浮点数)

1.22,3.2, 4.9,12.3.....and so on

And I want to find the smallest number greater than (lets say) 4.. So the answer is 4.9 But besides the obvious solution.. (iterating thru list and keeping a track of smallest number greater than k) what is the "pythonic way" to do this. 而且我想找到大于(比如说)4的最小数字。所以答案是4.9但除了明显的解决方案..(迭代通过列表并保持最小数量大于k的轨道)什么是“pythonic方式“ 去做这个。 Thanks 谢谢

min(x for x in my_list if x > 4)

Binary search would be a standard way to deal with this, but only if the list is sorted, as previous answer pointed out. 二进制搜索将是处理此问题的标准方法,但只有在列表被排序时,如前面的答案所指出的那样。

See Python binary search-like function to find first number in sorted list greater than a specific value 请参阅Python二进制搜索类函数,以查找排序列表中大于特定值的第一个数字

and In Python, how do you find the index of the first value greater than a threshold in a sorted list? 在Python中,如何在排序列表中找到第一个值大于阈值的索引?

for discussion of a module that does this for you: http://docs.python.org/library/bisect.html 讨论为您执行此操作的模块: http//docs.python.org/library/bisect.html

This is a perfect scenario for filter . 这是过滤器的完美场景。

>>> L = [1.22, 3.2, 4.9, 12.3]
>>> k = 4
>>> a = min(filter(lambda x: x > k, L))
>>> print(a)
4.9

You could also use list comprehensions: 您还可以使用列表推导:

>>> L = [1.22, 3.2, 4.9, 12.3]
>>> k = 4
>>> a = min([element for element in L if element > k])
>>> print(a)
4.9

Although the list comprehension seems to be less straight-forward on the first view, it is the recommended way to do it. 尽管列表理解在第一个视图中似乎不那么简单,但建议使用它。 According to some Python developers, filter should not be used. 根据一些Python开发人员的说法,不应该使用filter

A generator expression is even better because it doesn't create the list in memory: 生成器表达式甚至更好,因为它不会在内存中创建列表:

>>> L = [1.22, 3.2, 4.9, 12.3]
>>> k = 4
>>> a = min(element for element in L if element > k)
>>> print(a)
4.9

I've no idea about python, but from an algorithmic point of view maybe I can add something. 我不知道python,但从算法的角度来看,我可以添加一些内容。 In your example your list is monotonically increasing (sorted). 在您的示例中,您的列表单调递增(排序)。 If that is always true of your list, then a small optimization might be to stop iterating once you've reached a number larger than 4. 如果您的列表始终如此,则一旦您达到大于4的数字,一个小优化可能会停止迭代。

If your list always has few numbers lower than 4, this will be a great optimization, but if number of items before and after the target number is random, then this improvement isn't reliable. 如果您的列表总是少于4的数字,这将是一个很好的优化,但如果目标数字之前和之后的项目数是随机的,那么这种改进是不可靠的。

In that case, you might want to search the list by partitioning it. 在这种情况下,您可能希望通过对列表进行分区来搜索列表。 Test if middle element is larger than 4. If it is larger, throw away upper half, otherwise throw away lower half. 测试中间元素是否大于4.如果它更大,扔掉上半部分,否则扔掉下半部分。 Do the same thing on the new half-length list. 在新的半长名单上做同样的事情。 You need to deal with even and odd numbers and with the case when you have only 1 or 2 items left in the list-segment. 您需要处理偶数和奇数以及在列表段中只剩下1或2个项目的情况。 For a large list, this should reduce the number of tests significantly. 对于大型列表,这应该显着减少测试次数。

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

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