查找总和最接近给定数字的所有数字 python [英] Find all numbers that sum closest to a given number python

查看:65
本文介绍了查找总和最接近给定数字的所有数字 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有一个脚本,它给出了与给定总和最接近的 2 个值.它还遍历给定和的列表,并在每次迭代后删除已使用的数字.

I have a script below that gives the closest 2 values to a given sum. It also iterates through a list of given sums and after each iteration removes the numbers that have already been used.

我需要修改这个脚本,以便它生成最接近每个总和的必要数量的值,而不是 2.脚本需要接受浮点值并且不能重复使用值.实际上,它需要选择最接近目标的最有效的集合,更新该集合以删除使用的值,然后移动到下一个目标等..

I need to modify this script so that it produces a necessary amount of values closest to each sum, rather than 2. The script needs to accept float values and cannot re-use values. Effectively it needs to pick the most efficient set closest to target, update the set to remove values used, then move on to next target etc..

对于需要 3 个数字或 4 个数字等的特定用例/集合,它不能很好地工作,实际上最接近总和.我需要这个脚本也能够接受浮点值,这个脚本目前这样做.

With pairs it it doesn't work very well for specific use cases/sets that require 3 numbers or 4 etc. to actually get closest to the sum. I need this script to also be able to accept float values, which this script currently does.

任何建议将不胜感激.另外,如果有人知道更好的脚本,请告诉我.

Any suggestions would be much appreciated. Also if someone knows a better script for this, please let me know.

import sys
def find_closese_sum(numbers, target):
    start = 0
    end = len(numbers) - 1
    result = sys.maxint
    result_tuple = None
    while start < end:
        if numbers[start] + numbers[end] == target:
            print 0, (numbers[start], numbers[end])
            return
        elif numbers[start] + numbers[end] > target:
            if abs(numbers[start] + numbers[end] - target) < result:
                result = abs(numbers[start] + numbers[end] - target)
                result_tuple = (numbers[start], numbers[end])
            end -= 1
        else:
            if abs(numbers[start] + numbers[end] - target) < result:
                result = abs(numbers[start] + numbers[end] - target)
                result_tuple = (numbers[start], numbers[end])
            start += 1

    for i in result_tuple:
        numbers.remove(i)

    return result_tuple

if __name__ == "__main__":
    target = [14,27,39]
    numbers = [1,5,5,10,7,8,11,13,66,34]
    print numbers
    numbers = sorted(numbers)


    for i in target:
        result_shown = find_closese_sum(numbers, i)

        print result_shown

推荐答案

我的回答,使用 python 3,但你应该能够轻松移植它.

My answer, using python 3 but you should be able to port it easily.

from itertools import combinations as c

if __name__ == "__main__":
    target = [14,27,39]
    numbers = [1,5,5,10,7,8,11,13,66,34]

    for combo in range(1,4):
        for i in target:
            #lambda to find the difference between sum and target
            diff = lambda x: abs(sum(x) - i)
            #get all unique combinations
            combos = {tuple(sorted(c)) for c in c(numbers, combo)}
            #sort them
            combos = sorted(combos, key = diff)
            #get the smallest difference
            smallest = diff(combos[0])
            #filter out combos larger than the smaller difference
            result = [c for c in combos if diff(c) == smallest]
            print('results for {}, best combinations are off by {}:'.format(i, smallest))
            print(result)

您声明您需要排除用于先前结果的数字.为此,只需将它们从列表中删除:

You stated you need to exclude numbers used for a previous result. To do that just remove them from the list:

            #after print(result), inside the "for i in target" loop
            #if you want to exclude all numbers used in all combinations
            numbers = [n for n in numbers if n not in [b for a in result for b in a]]
            #if you only need to remove the first match
            numbers = [n for n in numbers if n not in result[0]]

这篇关于查找总和最接近给定数字的所有数字 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆