简体   繁体   中英

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)]

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~

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
  2. An integer value K where K > 0
  3. A list combo which is all possible combinations of integer values from the list lst
  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)
    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)
  5. Return combo

With that in mind lets take a look at what the code might look like: 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.

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

[{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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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