[英]How To Use Python to find combinations of value with equal or greater than given sum?
我在谷歌下面找到了一些代碼,我試圖修改它但失敗了
`
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))
`
結果是 [(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), (100), (101), (20, 30, 50)] 或 [(10, 90), (100), (101), (20, 40, 50)] 這也可以~
謝謝!
代碼附在上面/。
對於任何編程語言,最好將問題分解成更小的部分,以便在編寫代碼時我們確切地知道程序的預期內容。
根據您的問題,這是我想到的計划標准:
lst
K
,其中 K > 0combo
,它是列表lst
中 integer 個值的所有可能組合unused
的集合,其中包含lst
中尚未在combo
列表中使用的所有值,並將result
初始化為空列表(這將是輸出)result
,使其包含>= K
的值之和,並且每個值組合僅使用 integer 一次(即,當 integer 被使用一次時,它不能在另一個列表中使用)combo
考慮到這一點,讓我們看一下代碼的樣子: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))
當然,打印值是[(100,), (101,), (10, 90), (20, 30, 50)]
,這是您希望從 function 獲得的預期結果之一。
我完全知道這個解決方案可以小得多。 但我把它展開來展示我的思維過程以及我通常如何解決編程問題。 我希望解決方案和這個過程都能幫助你和任何其他偶然發現這個的程序員
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}]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.