-
-
Save zhoufankai/cf6400013e05654068860b5826b87913 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
给你一个列表,列表里的值都为整数,取出3个值相加为0的所有情况。保证取出来的值为不同组合,如 [1,2,3]已有,你可以取[3,2,1],但不能再取[1,2,3]. | |
result = [] | |
result_by_set = [] | |
l = [1,2,-1,3,-3,6,0] | |
for i, j in enumerate(l): | |
for m, n in enumerate(l[i+1:]): | |
for p in l[m+1:]: | |
if j + n + p == 0: | |
value = [j, n, p] | |
value_set = set(value) | |
if value_set in result_by_set: | |
continue | |
result_by_set.append(value_set) | |
result.append(value) | |
print result_by_set | |
print result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment