2017-09-20 15 views

回答

0

您可以生成所有在你原来的列表中的2-值组合

>>> a = [7,8,8,9,4] 
>>> import itertools 
>>> list(itertools.combinations(a, 2)) 
[(7, 8), (7, 8), (7, 9), (7, 4), (8, 8), (8, 9), (8, 4), (8, 9), (8, 4), (9, 4)] 

然后使用列表理解来检查,如果他们的总和等于你的价值

>>> total = 15 
>>> [i for i in itertools.combinations(a, 2) if sum(i) == total] 
[(7, 8), (7, 8)] 
相关问题