Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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_Python 2.7_Python 3.x - Fatal编程技术网

Python 如何找到整数列表的所有子集?

Python 如何找到整数列表的所有子集?,python,python-2.7,python-3.x,Python,Python 2.7,Python 3.x,我编写了下面的代码来从列表S中查找2个整数的子集 import itertools S = [1, 2, 3, 4, 6] subsets = itertools.combinations(S, 2) print subsets 我期待以下结果: [[1, 2], [1, 3], ... [4, 6]] 我从上面的代码中得到一个错误,如下所示: Traceback (most recent call last): Line 5, in <module> subse

我编写了下面的代码来从列表S中查找2个整数的子集

import itertools

S = [1, 2, 3, 4, 6]

subsets = itertools.combinations(S, 2)
print subsets
我期待以下结果:

[[1, 2], [1, 3], ... [4, 6]]
我从上面的代码中得到一个错误,如下所示:

Traceback (most recent call last):
  Line 5, in <module>
    subsets = itertools.combinations(S, 2)
AttributeError: 'module' object has no attribute 'combinations'
回溯(最近一次呼叫最后一次):
第5行,在
子集=itertools.组合(S,2)
AttributeError:“模块”对象没有属性“组合”

为什么我无法导入组合()

从您的问题描述来看,您似乎正在使用Python 2.5或更低版本。因为只有在Python2.6之后才可用,所以必须升级到更新的版本才能使用它


您可以使用
python--version
找出当前使用的版本。

从您的问题描述中,您似乎正在使用python 2.5或更低版本。因为只有在Python2.6之后才可用,所以必须升级到更新的版本才能使用它


您可以使用
python--version
找出您当前使用的版本。

您使用的是哪个版本的python?您使用的是哪个版本的python?