1、数据表 num_list.xlsx
Num |
---|
11 |
22 |
33 |
44 |
55 |
2、获取一个值在列表里相加的所有组合
import itertools
import pandas as pd
import numpy as np
num_data=pd.read_excel('num_list.xlsx',header=0, encoding='utf-8')
num_data1 = np.array(num_data)
num_x_list = num_data1.tolist()
num_result = sum(num_x_list, [])
def get_result(hope, list1):
""" :param hope: # 期望相加所得参数 :param list1: # 所有数值 :return: """
result = [] # 结果列表
for i in range(1, len(list1) + 1):
iter = itertools.combinations(list1, i) # 求每个长度的组合
group_item = list(iter)
for x in range(0, len(group_item)):
if sum(group_item[x]) == hope:
result.append(group_item[x])
print('有', len(result), '种情况得出结果为', hope, ',组合是', result)
if __name__ == '__main__':
get_result(55, num_result)
3、运行结果
>>有 3 种情况得出结果为 55 ,组合是 [(55,), (11, 44), (22, 33)]