Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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_Algorithm_Binary - Fatal编程技术网

如何在python中查看给定数字的可能组合

如何在python中查看给定数字的可能组合,python,algorithm,binary,Python,Algorithm,Binary,在Python中,我希望看到所有可能的数字组合,但限于0和1 例如,某个循环的结果是: 0000 0001 0011 0111 1111 1000 and so on. 哪种python算法最适合这种情况?示例如下: 例如: 看发电机 该模块实现了许多迭代器构建块 通过来自APL、Haskell和SML的构造。每一个都在一个 适合Python的表单 该模块标准化了一套快速、高效内存的核心工具 单独使用或组合使用的。它们一起形成 一种“迭代器代数”,使构造专门的 纯Python中简洁高效的工具

在Python中,我希望看到所有可能的数字组合,但限于0和1

例如,某个循环的结果是:

0000
0001
0011
0111
1111
1000
and so on.
哪种python算法最适合这种情况?

示例如下:

例如:

看发电机

该模块实现了许多迭代器构建块 通过来自APL、Haskell和SML的构造。每一个都在一个 适合Python的表单

该模块标准化了一套快速、高效内存的核心工具 单独使用或组合使用的。它们一起形成 一种“迭代器代数”,使构造专门的 纯Python中简洁高效的工具

.

参见发电机

该模块实现了许多迭代器构建块 通过来自APL、Haskell和SML的构造。每一个都在一个 适合Python的表单

该模块标准化了一套快速、高效内存的核心工具 单独使用或组合使用的。它们一起形成 一种“迭代器代数”,使构造专门的 纯Python中简洁高效的工具


.

您正在寻找k组合。退房

您要查看的函数是xcombinations:

def xcombinations(items, n):
    if n==0: yield []
    else:
        for i in xrange(len(items)):
            for cc in xcombinations(items[:i]+items[i+1:],n-1):
                yield [items[i]]+cc

你在寻找k-组合。退房

您要查看的函数是xcombinations:

def xcombinations(items, n):
    if n==0: yield []
    else:
        for i in xrange(len(items)):
            for cc in xcombinations(items[:i]+items[i+1:],n-1):
                yield [items[i]]+cc

非常感谢大家的回答。结果我看到的是灰色代码。弗兰克·格雷之后的格雷码是一种二进制数字系统,其中两个连续值仅在一位上不同。维基百科非常感谢大家的回答。结果我看到的是灰色代码。弗兰克·格雷之后的格雷码是一种二进制数字系统,其中两个连续值仅在一位上不同。Wikipedia谢谢你的链接,我在别处需要的关于生成器和组合的大量信息。谢谢你的链接,我在别处需要的关于生成器和组合的大量信息。
def f(n):
   if n==1:
       return ['0', '1']
   tmp = f(n-1)
   return ['0'+v for v in tmp] + ['1'+v for v in tmp]

>>> f(4)
['0000',
'0001',
'0010',
'0011',
'0100',
'0101',
'0110',
'0111',
'1000',
'1001',
'1010',
'1011',
'1100',
'1101',
'1110',
'1111']
def xcombinations(items, n):
    if n==0: yield []
    else:
        for i in xrange(len(items)):
            for cc in xcombinations(items[:i]+items[i+1:],n-1):
                yield [items[i]]+cc