简体   繁体   English

如何使用 Python 查找等于或大于给定总和的值组合?

[英]How To Use Python to find combinations of value with equal or greater than given sum?

I find some codes below at google and I tried to modified it but failed我在谷歌下面找到了一些代码,我试图修改它但失败了

` `

from itertools import combinations
  
def findPairs(lst, K):
      
    return [pair for pair in combinations(lst, 2) if ( sum(pair) >= K or sum(pair) == K )]
      
# Driver code
lst = [10, 20, 30, 40, 50, 90, 100, 101]
K = 100
print(findPairs(lst, K))

` `

The result turn out is [(10, 90), (10, 100), (10, 101), (20, 90), (20, 100), (20, 101), (30, 90), (30, 100), (30, 101), (40, 90), (40, 100), (40, 101), (50, 90), (50, 100), (50, 101), (90, 100), (90, 101), (100, 101)]结果是 [(10, 90), (10, 100), (10, 101), (20, 90), (20, 100), (20, 101), (30, 90), (30 , 100), (30, 101), (40, 90), (40, 100), (40, 101), (50, 90), (50, 100), (50, 101), (90, 100 ), (90, 101), (100, 101)]

But what I want is without duplicated used of numbers.但我想要的是不重复使用数字。 [(10, 90), (100), (101), (20, 30, 50)] or [(10, 90), (100), (101), (20, 40, 50)] this also ok~ [(10, 90), (100), (101), (20, 30, 50)] 或 [(10, 90), (100), (101), (20, 40, 50)] 这也可以~

Thank you!谢谢!

Codes attached at above/.代码附在上面/。

With any programming language its best to break down the problem into smaller pieces so when it is time to code we know exactly what is expected of the program.对于任何编程语言,最好将问题分解成更小的部分,以便在编写代码时我们确切地知道程序的预期内容。

Based on your question this is the program criteria I have in mind:根据您的问题,这是我想到的计划标准:

  1. A list lst of integer values integer 个值的列表lst
  2. An integer value K where K > 0 integer 值K ,其中 K > 0
  3. A list combo which is all possible combinations of integer values from the list lst列表combo ,它是列表lst中 integer 个值的所有可能组合
  4. Create a set unused which contains all values from lst that haven't been used in a list in combo yet and initialize result to an empty list (this will be the output)创建一个unused的集合,其中包含lst中尚未在combo列表中使用的所有值,并将result初始化为空列表(这将是输出)
    1. Add to result so it contains sums of values >= K and such that each combination of values uses an integer exactly once (ie, when the integer is used once it can't be used in another list)添加到result ,使其包含>= K的值之和,并且每个值组合仅使用 integer 一次(即,当 integer 被使用一次时,它不能在另一个列表中使用)
  5. Return combo返回combo

With that in mind lets take a look at what the code might look like: import itertools考虑到这一点,让我们看一下代码的样子:import itertools

def findPairs(lst, K):
    # 1. A list `lst` of integer values
    # NOTE: this section is optional, 
    # but it's a good habit to check the input in Python
    if not isinstance(lst, list) or not all(isinstance(x, int) for x in lst):
        return []
    # 2. An integer value `K` where K > 0
    # NOTE: this section is optional,
    # but it's a good habit to check the input in Python
    if K <= 0 or not isinstance(K, int):
        return []
    # 3. A list `combo` which is all possible combinations
    #    of integer values from the list `lst`
    # Read more about itertools.combinations at 
    # https://docs.python.org/3/library/itertools.html
    combo = []
    for i in range(len(lst) + 1):
        for subset in itertools.combinations(lst, i):
            combo.append(subset)
    # 4. Create a set `unused` which contains all values 
    #    from `lst` that haven't been used in a
    #    list in `combo` yet and initialize `result`
    #    to an empty list (this will be the output)
    unused = set(lst)
    result = []
    # 5. Add to `result` so it contains sums of values `>= K`
    #    and such that each combination of values
    #    uses an integer exactly once (i.e., when the integer
    #    is used once it can't be used in another list)
    for i in combo:
        if (sum(i) >= K) and all(j in unused for j in i):
                unused -= set(i)
                result.append(i)
    # 6. Return `result`
    return result
    
lst = [10, 20, 30, 40, 50, 90, 100, 101]
K = 100
print(findPairs(lst, K))

And of course, the printed value is [(100,), (101,), (10, 90), (20, 30, 50)] which is one of the expected results you wanted from the function.当然,打印值是[(100,), (101,), (10, 90), (20, 30, 50)] ,这是您希望从 function 获得的预期结果之一。

I am fully aware this solution can be much, much smaller.我完全知道这个解决方案可以小得多。 But I spread it out to show my thought process and how I typically solve programming problems.但我把它展开来展示我的思维过程以及我通常如何解决编程问题。 I hope both the solution and this process helps you out and any other programmers who stumble upon this我希望解决方案和这个过程都能帮助你和任何其他偶然发现这个的程序员

from itertools import compress, product
  
def findPairs(lst, K):
    return [pair for pair in (set(compress(lst,mask)) for mask in product(*[[0,1]]*len(lst))) if sum(pair) >= K ]
      
# Driver code
lst = [10, 20, 30, 40, 50, 90, 100, 101]
K = 100
print(findPairs(lst, K))

output output

[{101}, {100}, {100, 101}, {90, 101}, {90, 100}, {90, 100, 101}, {50, 101}, {50, 100}, {50, 100, 101}, {50, 90}, {50, 101, 90}, {50, 100, 90}, {50, 101, 100, 90}, {40, 101}, {40, 100}, {40, 100, 101}, {40, 90}, {40, 90, 101}, {40, 90, 100}, {40, 90, 100, 101}, {40, 50, 101}, {40, 50, 100}, {40, 50, 100, 101}, {40, 50, 90}, {40, 50, 101, 90}, {40, 50, 100, 90}, {100, 101, 40, 50, 90}, {101, 30}, {100, 30}, {100, 101, 30}, {90, 30}, {90, 101, 30}, {90, 100, 30}, {90, 100, 101, 30}, {50, 101, 30}, {50, 100, 30}, {50, 100, 101, 30}, {50, 90, 30}, {50, 101, 90, 30}, {50, 100, 90, 30}, {100, 101, 50, 90, 30}, {40, 101, 30}, {40, 100, 30}, {40, 100, 101, 30}, {40, 90, 30}, {40, 90, 101, 30}, {40, 90, 100, 30}, {100, 101, 40, 90, 30}, {40, 50, 30}, {40, 50, 101, 30}, {40, 50, 100, 30}, {100, 101, 40, 50, 30}, {40, 50, 90, 30}, {101, 40, 50, 90, 30}, {100, 40, 50, 90, 30}, {100, 101, 40, 50, 90, 30}, {20, 101}, {100, 20}, {100, 20, 101}, {90, 20}, {90, 20, 101}, {100, 90, 20}, {100, 90, 20, 101}, {50, 20, 101}, {100, 50, 20}, {100, 50, 20, 101}, {50, 20, 90}, {50, 101, 20, 90}, {100, 50, 20, 90}, {100, 101, 50, 20, 90}, {40, 20, 101}, {40, 100, 20}, {40, 100, 20, 101}, {40, 90, 20}, {40, 90, 20, 101}, {40, 100, 90, 20}, {100, 101, 40, 20, 90}, {40, 50, 20}, {40, 50, 20, 101}, {40, 100, 50, 20}, {100, 101, 40, 50, 20}, {40, 50, 20, 90}, {101, 40, 50, 20, 90}, {100, 40, 50, 20, 90}, {100, 101, 40, 50, 20, 90}, {20, 101, 30}, {100, 20, 30}, {100, 20, 101, 30}, {90, 20, 30}, {90, 20, 101, 30}, {100, 90, 20, 30}, {100, 101, 20, 90, 30}, {50, 20, 30}, {50, 20, 101, 30}, {100, 50, 20, 30}, {100, 101, 50, 20, 30}, {50, 20, 90, 30}, {101, 50, 20, 90, 30}, {100, 50, 20, 90, 30}, {100, 101, 50, 20, 90, 30}, {40, 20, 101, 30}, {40, 100, 20, 30}, {100, 101, 40, 20, 30}, {40, 90, 20, 30}, {101, 40, 20, 90, 30}, {100, 40, 20, 90, 30}, {100, 101, 40, 20, 90, 30}, {40, 50, 20, 30}, {101, 40, 50, 20, 30}, {100, 40, 50, 20, 30}, {100, 101, 40, 50, 20, 30}, {40, 50, 20, 90, 30}, {101, 40, 50, 20, 90, 30}, {100, 40, 50, 20, 90, 30}, {100, 101, 40, 50, 20, 90, 30}, {10, 101}, {10, 100}, {10, 100, 101}, {10, 90}, {10, 101, 90}, {10, 100, 90}, {10, 101, 100, 90}, {10, 50, 101}, {100, 10, 50}, {100, 10, 50, 101}, {10, 50, 90}, {10, 101, 50, 90}, {100, 10, 50, 90}, {100, 101, 10, 50, 90}, {40, 10, 101}, {40, 10, 100}, {40, 10, 100, 101}, {40, 10, 90}, {40, 10, 101, 90}, {40, 10, 100, 90}, {100, 101, 40, 10, 90}, {40, 10, 50}, {40, 10, 50, 101}, {40, 100, 10, 50}, {100, 101, 40, 10, 50}, {40, 10, 50, 90}, {101, 40, 10, 50, 90}, {100, 40, 10, 50, 90}, {100, 101, 40, 10, 50, 90}, {10, 101, 30}, {10, 100, 30}, {10, 100, 101, 30}, {10, 90, 30}, {10, 101, 90, 30}, {10, 100, 90, 30}, {100, 101, 10, 90, 30}, {10, 50, 101, 30}, {100, 10, 50, 30}, {100, 101, 10, 50, 30}, {10, 50, 90, 30}, {101, 10, 50, 90, 30}, {100, 10, 50, 90, 30}, {100, 101, 10, 50, 90, 30}, {40, 10, 101, 30}, {40, 10, 100, 30}, {100, 101, 40, 10, 30}, {40, 10, 90, 30}, {101, 40, 10, 90, 30}, {100, 40, 10, 90, 30}, {100, 101, 40, 10, 90, 30}, {40, 10, 50, 30}, {101, 40, 10, 50, 30}, {100, 40, 10, 50, 30}, {100, 101, 40, 10, 50, 30}, {40, 10, 50, 90, 30}, {101, 40, 10, 50, 90, 30}, {100, 40, 10, 50, 90, 30}, {100, 101, 40, 10, 50, 90, 30}, {10, 20, 101}, {100, 10, 20}, {100, 10, 20, 101}, {10, 20, 90}, {10, 101, 20, 90}, {100, 10, 20, 90}, {100, 101, 10, 20, 90}, {10, 101, 20, 50}, {100, 10, 20, 50}, {100, 101, 10, 50, 20}, {10, 90, 20, 50}, {101, 10, 50, 20, 90}, {100, 10, 50, 20, 90}, {100, 101, 10, 50, 20, 90}, {40, 10, 20, 101}, {40, 100, 10, 20}, {100, 101, 40, 10, 20}, {40, 10, 20, 90}, {101, 40, 10, 20, 90}, {100, 40, 10, 20, 90}, {100, 101, 40, 10, 20, 90}, {40, 10, 20, 50}, {101, 40, 10, 50, 20}, {100, 40, 10, 50, 20}, {100, 101, 40, 10, 50, 20}, {40, 10, 50, 20, 90}, {101, 40, 10, 50, 20, 90}, {100, 40, 10, 50, 20, 90}, {100, 101, 40, 10, 50, 20, 90}, {10, 20, 101, 30}, {100, 10, 20, 30}, {100, 101, 10, 20, 30}, {10, 20, 90, 30}, {101, 10, 20, 90, 30}, {100, 10, 20, 90, 30}, {100, 101, 10, 20, 90, 30}, {10, 20, 50, 30}, {101, 10, 50, 20, 30}, {100, 10, 50, 20, 30}, {100, 101, 10, 50, 20, 30}, {10, 50, 20, 90, 30}, {101, 10, 50, 20, 90, 30}, {100, 10, 50, 20, 90, 30}, {100, 101, 10, 50, 20, 90, 30}, {40, 10, 20, 30}, {101, 40, 10, 20, 30}, {100, 40, 10, 20, 30}, {100, 101, 40, 10, 20, 30}, {40, 10, 20, 90, 30}, {101, 40, 10, 20, 90, 30}, {100, 40, 10, 20, 90, 30}, {100, 101, 40, 10, 20, 90, 30}, {40, 10, 50, 20, 30}, {101, 40, 10, 50, 20, 30}, {100, 40, 10, 50, 20, 30}, {100, 101, 40, 10, 50, 20, 30}, {40, 10, 50, 20, 90, 30}, {101, 40, 10, 50, 20, 90, 30}, {100, 40, 10, 50, 20, 90, 30}, {100, 101, 40, 10, 50, 20, 90, 30}]

reference: https://stackoverflow.com/a/6542458/9526787参考: https://stackoverflow.com/a/6542458/9526787

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

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