Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 列出给定数字集合的组合及其总数_Python_Combinations - Fatal编程技术网

Python 列出给定数字集合的组合及其总数

Python 列出给定数字集合的组合及其总数,python,combinations,Python,Combinations,我使用这段代码从一组给定的列表中生成三个数字的组合 import itertools thelist = [0, 0, 0, 1, 1, 2, 2, 1] for L in range(3,4): for subset in itertools.combinations(thelist, L): print(subset) 这很好,结果如下所示: (0, 0, 0) (0, 0, 1) (0, 0, 1) (0, 0, 2) (0, 0, 2) (0, 0, 1) (

我使用这段代码从一组给定的列表中生成三个数字的组合

import itertools

thelist = [0, 0, 0, 1, 1, 2, 2, 1]
for L in range(3,4):
    for subset in itertools.combinations(thelist, L):
        print(subset)
这很好,结果如下所示:

(0, 0, 0)
(0, 0, 1)
(0, 0, 1)
(0, 0, 2)
(0, 0, 2)
(0, 0, 1)
(0, 0, 1)
(0, 0, 1)
(0, 0, 2)
(0, 0, 2)
(0, 0, 1)
(0, 1, 1)
(0, 1, 2)
(0, 1, 2)
(0, 1, 1)
(0, 1, 2)
(0, 1, 2)
(0, 1, 1)
(0, 0, 0) =>0
(0, 0, 1) =>1
(0, 0, 1) =>1
(0, 0, 2) =>2
(0, 0, 2) =>2
(0, 0, 1) =>1
(0, 0, 1) =>1
(0, 0, 1) =>1
(0, 0, 2) =>2
(0, 0, 2) =>2
(0, 0, 1) =>1
(0, 1, 1) =>2
我想列出生产组合的总和。我该怎么做? 因此,输出将如下所示:

(0, 0, 0)
(0, 0, 1)
(0, 0, 1)
(0, 0, 2)
(0, 0, 2)
(0, 0, 1)
(0, 0, 1)
(0, 0, 1)
(0, 0, 2)
(0, 0, 2)
(0, 0, 1)
(0, 1, 1)
(0, 1, 2)
(0, 1, 2)
(0, 1, 1)
(0, 1, 2)
(0, 1, 2)
(0, 1, 1)
(0, 0, 0) =>0
(0, 0, 1) =>1
(0, 0, 1) =>1
(0, 0, 2) =>2
(0, 0, 2) =>2
(0, 0, 1) =>1
(0, 0, 1) =>1
(0, 0, 1) =>1
(0, 0, 2) =>2
(0, 0, 2) =>2
(0, 0, 1) =>1
(0, 1, 1) =>2

如果您只是在python
=3.6
中打印,则可以使用f字符串,只需执行以下操作:

import itertools

thelist = [0, 0, 0, 1, 1, 2, 2, 1]
for L in range(3,4):
    for subset in itertools.combinations(thelist, L):
        print(f"{subset} => {sum(subset)}")

对于Python
,如果您只是在Python
=3.6
中打印,则可以使用f字符串,只需执行以下操作:

import itertools

thelist = [0, 0, 0, 1, 1, 2, 2, 1]
for L in range(3,4):
    for subset in itertools.combinations(thelist, L):
        print(f"{subset} => {sum(subset)}")
对于Python