简体   繁体   English

如何在列表中找到比我的号码大的最接近的号码?

[英]How do I find the closest number in a list that is greater than my number?

I want to get the closest number (or the index of it) in a list that is greater than a given number.我想在列表中获取大于给定数字的最接近的数字(或它的索引)。 The list:名单:

lst=[1,2,5]

My number:我的号码:

num=3

If I use: min(lst, key=lambda x:abs(x-num)) it will give 2 but I want it to give me 5如果我使用: min(lst, key=lambda x:abs(x-num))它会给我2但我希望它给我5

You need to consider numbers greater than num :您需要考虑大于num的数字:

output, index = min((i, idx) for idx, i in enumerate(lst) if i>num)

You could write an actual loop to do it:您可以编写一个实际的循环来执行此操作:

closest_greater = None
for item in lst:
    if item > num and (closest_greater is None or item < closest_greater):
        closest_greater = item

gives closest_greater = 5给出最接近的closest_greater = 5

Alternatively, you could give the min() function a generator expression that includes only elements that are greater than num或者,您可以给min() function 一个生成器表达式,其中仅包含大于num的元素

min(i for i in lst if i > num)

Would you like to use a binary sort?你想使用二进制排序吗? In my tests, this is slightly faster.在我的测试中,这稍微快一些。

import time
from bisect import bisect


def timer(func):
    def function_timer(*args, **kwargs):
        start = time.time()
        func(*args, **kwargs)
        print "The runtime took {} seconds to complete".format(round(time.time() - start, 2))

    return function_timer


test_list = [1, 7, 5, 6, 3, 8]
k = 3

@timer
def t1():
    for x in range(1000000):
        test_list[bisect(test_list, k)]


@timer
def t2():
    for x in range(1000000):
        min((i, idx) for idx, i in enumerate(test_list) if i > k)

yields产量

The runtime took 0.22 seconds to complete
The runtime took 0.94 seconds to complete

暂无
暂无

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

相关问题 如何将列表和用户号码传递给函数并让它显示列表中大于用户号码的所有数字? - How do I pass both the list and the user's number to a function and have it display all numbers in the list that are greater than the user's number? 在列表中找到连续数字大于“ n”的最后一个数字 - find the last number in list with consecutive number of numbers greater than “n” 在排序列表中查找大于给定数字的最小数字 - Find the smallest number that is greater than a given number in a sorted list 如何在熊猫中选择和存储大于数字的列? - How do I select and store columns greater than a number in pandas? 如何计算列表中的数字大于其后的数字的次数? - How to count the number of times a number in a list is greater than the number after it? 如何在 Pandas 系列中找到与输入数字最接近的值? - How do I find the closest values in a Pandas series to an input number? 如何在python的列表中找到最小的最接近数字 - How to find the smallest closest number in a list in python 如何找到列表的顺序,使其元素大于第二个列表中的相应元素。 我必须最大化这样的数字 - How to find an order of list such that its elements are greater than the corresponding elements in second list. I have to maximize such number 找到等于或大于给定数字的最接近的数字 - Finding the closest number equal to or greater than a given number 如何检查列表中的数字是否大于它之前的数字-python - How to check if a number in a list is greater than the number before it - python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM