list1 = [5,8]
list2 = [4,4,2,3,6]
这是很容易通过使用powerset函数
def powerset(iterable):
"powerset([1,2,3]) -->() (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
s = list(iterable)
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
8可以由[4,4]
或[2,6]
得到在list2
的5和8的组合,但5只能是由[2,3]
组成。如果我选择[2,6]
为8,则list2
中没有5的组合。
如何获得[4,4]
8和[2,3]
5?我想在list1
中选择尽可能多的list2
组合。实际上list1
中的数字可能由list2
中的3个或更多数字组成。
实际问题比较困难,因为可能有一些数字在list1
中未使用,而list1
中的数字可能包含3个或更多数字。
这是怎么回事? –
非常感谢,但如果选择[2,6]为8,则列表2中没有可能的组合5,这是我的问题。我想为列表1中的数字尽可能选择list2中的组合。 – goldmonkey