Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从一组数字中找出长度为3的所有组合,使总和(组合)为0_Python_Permutation_List Comprehension - Fatal编程技术网

Python 从一组数字中找出长度为3的所有组合,使总和(组合)为0

Python 从一组数字中找出长度为3的所有组合,使总和(组合)为0,python,permutation,list-comprehension,Python,Permutation,List Comprehension,假设S是一组整数,例如{-4,-2,1,2,5,0}。我想写一个理解,以便得到所有三个元素元组的列表(I,j,k),这样I,j,k是S的元素,I+j+k==0实际上为组合显示了等效的python代码 def combinations(iterable, r): # combinations('ABCD', 2) --> AB AC AD BC BD CD # combinations(range(4), 3) --> 012 013 023 123 pool

假设S是一组整数,例如
{-4,-2,1,2,5,0}
。我想写一个理解,以便得到所有三个元素元组的列表
(I,j,k)
,这样
I,j,k
是S的元素,
I+j+k==0

实际上为组合显示了等效的python代码

def combinations(iterable, r):
    # combinations('ABCD', 2) --> AB AC AD BC BD CD
    # combinations(range(4), 3) --> 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = range(r)
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)

print [ x for x in combinations(s,3) if sum(x)==0]
[(0, 2, -2)]
原职:

就我理解你的问题和我的评论的答案而言,
I,j,k
中的任何值都可能是你设置的
S
中的任何值

>>> [(i,j,k) for i in S for j in S for k in S if i+j+k == 0]
[(0, 0, 0), (0, 2, -2), (0, -2, 2), (1, 1, -2), (1, -2, 1), (2, 0, -2), (2, 2, -4), (2, -4, 2), (2, -2, 0), (-4, 2, 2), (-2, 0, 2), (-2, 1, 1), (-2, 2, 0)]

编辑

由于问题已澄清,即同一值不能选择两次,因此此答案需要更新

>>> from itertools import permutations, combinations
>>> S={-4,-2,1,2,5,0}
>>> [x for x in permutations(S,3) if sum(x) == 0]
[(0, 2, -2), (0, -2, 2), (2, 0, -2), (2, -2, 0), (-2, 0, 2), (-2, 2, 0)]
>>> [x for x in combinations(S,3) if sum(x) == 0]
[(0, 2, -2)]

使用
排列
组合
(我还没有弄清楚您想要哪一个)。

假设同一个数字不能多次用于计算总和:

from itertools import combinations

S = {-4, -2, 1, 2, 5, 0}
zero_sums = [(i, j, k) for i, j, k in combinations(S, 3) if not sum((i, j, k))]
print(zero_sums)  # -> [(0, 2, -2)]
您可以通过打印所有组合及其总和来验证这一点是否正确:

for i, j, k in combinations(S, 3):
    print('({:2d}, {:2d}, {:2d}) = {:2d}'.format(i, j, k, sum((i, j, k))))
输出:

(0,1,2)=3
( 0,  1,  5) =  6
( 0,  1, -4) = -3
( 0,  1, -2) = -1
( 0,  2,  5) =  7
( 0,  2, -4) = -2
( 0,  2, -2) =  0
( 0,  5, -4) =  1
( 0,  5, -2) =  3
( 0, -4, -2) = -6
( 1,  2,  5) =  8
( 1,  2, -4) = -1
( 1,  2, -2) =  1
( 1,  5, -4) =  2
( 1,  5, -2) =  4
( 1, -4, -2) = -5
( 2,  5, -4) =  3
( 2,  5, -2) =  5
( 2, -4, -2) = -4
( 5, -4, -2) = -1

我们不会做你的家庭作业。你需要先表现出你自己的努力。你自己也尝试了一些东西?欢迎来到stack overflow,看起来你已经收到了学校的作业,或者希望我们为你做所有的工作。尽管我们愿意尽一切可能提供帮助,但我们希望你自己也能表现出一些努力。。到目前为止你试过什么?你有没有遇到我们可以帮助解决的错误?向我们展示你的尝试,我们可以让你找到正确的方向,但stackoverflow不是为你编写所有代码或为你查找库的目的。i、j、k必须是S的不同元素吗?我没有做任何作业,只是在python中陷入了一些线性代数计算。因此,在不使用任何库的情况下,需要您的帮助@timgeb i,j,k可以是示例集合中的任何一个。我认为
i,j,k
中的任何值都可能是集合中的任何值(检查对问题的评论)@timgeb组合和置换是两个不同的东西,OP在问题中使用组合,因此我提供了一个组合解决方案。我错误地认为,允许从
S
中拾取相同的数字最多3次…如图所示,例如,在第一组数字
(0,0,0)
中。OP问题中的
S
只有一个
0
。@martineau我要求OP澄清同一个数字是否可以选择多次,就我理解他的答案而言,这种行为是允许的。@timgeb同一个数字不能选择多次。它最多只能选择一次,但三个元组之和必须等于0。