我想让这个数集里的其中5项,验证他们相加和有无0。需要改一下哪个位置?
id pc num
0 1 7 1
1 2 1 2
2 3 3 3
3 4 7 1
4 5 7 1
5 6 7 2
6 7 7 3
7 8 7 1
8 9 3 2
9 10 1 6
10 11 1 4
现在是所有子集相加为0的情况,我想让这个数集里的其中三项,验证他们相加和有无0
[-6,4,2]
可以用内置函数,找出所有三个数的组合,然后计算其相加是否为0
def hasZeroSumSublist(nums):
from itertools import combinations
for i in combinations(nums, 3):
if sum(i)==0:
return True
return False