凑数求和

示例列举出列表L=[1,2,3,4,5,6,8,8,9,7,5]里所有组合的加和为30的非重复的组合。

# -*- coding: utf-8 -*-
"""
Created on Thu Jul 19 22:19:37 2018

@author: FanXiaoLei
"""
import itertools
L=[1,2,3,4,5,6,8,8,9,7,5]
res=[]
for r in range(len(L)):
    bn=itertools.combinations(L,r+1)
    for b in bn:
        if sum(b)==30:
          if b not in res:
              res.append(b)
print(res)

结果如下图所示:

猜你喜欢

转载自blog.csdn.net/qq_24499417/article/details/81323523
今日推荐