[英]Finding the closest number equal to or greater than a given number
I have a python function in which I need to find the number in a list 'values' that's closest to a give number 'n' but that's greater than or equal to 'n'.我有一个 python 函数,我需要在列表“值”中找到最接近给定数字“n”但大于或等于“n”的数字。
So far I have this:到目前为止,我有这个:
def nearest_largest_value2 (n, values):
closest = []
for i in values:
if i == n:
closest = i
elif (i > n) and (i-n < 2):
closest = i
return closest
print(nearest_largest_value2(5, [1,3,4,6,7]))
print(nearest_largest_value2(5, [7,6,4,3,1]))
print(nearest_largest_value2(5, [1,3,4,5,6,7]))
Problem is that I'm getting the answer I want for the first two print statements (6) but I'm getting '6' for the last print statement when I want to get 5.问题是我得到了前两个打印语句(6)的答案,但是当我想得到 5 时,最后一个打印语句得到了“6”。
I'm new to Python but I thought once the first if
clause was satisfied the code would stop.我是 Python 新手,但我认为一旦满足第一个if
子句,代码就会停止。
The condition in < 2
is wrong. in < 2
中的条件是错误的。 Instead, you should be checking if the current i
is closer than the current closest
.相反,您应该检查当前i
是否比当前closest
。 Eg:例如:
def nearest_largest_value2 (n, values):
closest = None
for i in values:
if (i >= n) and (closest is None or i < closest):
closest = i
return closest
EDIT:编辑:
An alternative way to describe the problem is to find the minimal value that's larger or equal than n
.描述问题的另一种方法是找到大于或等于n
的最小值。 Describing the problem like that allows for a more "pythonic" oneliner:描述这样的问题允许一个更“pythonic”的单行:
def nearest_largest_value2 (n, values):
return min(v for v in values if v >= n)
EDIT2:编辑2:
As @ekhumoro pointed out, the alternative solution presented in the previous edit will break if values
does not contain any element that is equal to or greater than n
.正如@ekhumoro 指出的那样,如果values
不包含任何等于或大于n
的元素,则先前编辑中提出的替代解决方案将中断。 He also graciously offered a fix for it:他还慷慨地提出了解决办法:
def nearest_largest_value2 (n, values):
min([v for v in values if v >= n] or [None])
If you have multiple queries with different numbers, but to the same list , then it's more efficient to first sort the list and then do a binary search on it.如果您有多个不同数字的查询,但对同一个列表,那么首先对列表进行排序然后对其进行二进制搜索会更有效。
from bisect import bisect_left
values = [7, 6, 4, 3, 1]
values.sort()
def nearest_largest_value2 (n, values):
try:
result = values [bisect_left (values, n)]
except IndexError: # when number is > every element of list
result = None
return result
print (nearest_largest_value2 (5, values)) # 6
print (nearest_largest_value2 (3, values)) # 3
print (nearest_largest_value2 (8, values)) # None
Let p
correspond to length of 'values' list
and q
correspond to the number of inputs 'n'
(or # times nearest_largest_value2
is invoked).令p
对应于length of 'values' list
q
对应于number of inputs 'n'
(或 # 次最近的nearest_largest_value2
被调用)。
In the stated case , Time Complexity of the previous solution is O(pq)
whereas Time Complexity of the current solution is O((p + q)logp)
.在所述情况下,先前解决方案的时间复杂度为O(pq)
而当前解决方案的时间复杂度为O((p + q)logp)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.