简体   繁体   English

Python - 生成给定长度的1和0的所有可能数组的算法

[英]Python - Algorithm to generate all possible arrays of ones and zeros of a given length

My goal is to come up with an array of all possible bit combinations of length n. 我的目标是提出一个长度为n的所有可能位组合的数组。 For example, if n = 3, the target answer set should look like 例如,如果n = 3,则目标答案集应如下所示

000,
001,
010,
100,
011,
101,
110,
111

I've found the algorithmic solution , by I have completely no experience in iterators and C++ in general. 我找到了算法解决方案 ,我完全没有迭代器和C ++的经验。 Could someone give a hint how to rewrite the next function in python? 有人可以提示如何在python中重写下一个函数吗?

>>> import itertools
>>> result = ["".join(item) for item in itertools.product("01", repeat=3)]
>>> print result
['000', '001', '010', '011', '100', '101', '110', '111']

Without itertools: print the numbers from 0 to 2 ** n in base 2 and pad them with zeros: 没有itertools:在基数2中打印02 ** n的数字并用零填充它们:

for i in range(2 ** n):
    print('{0:b}'.format(i).rjust(n, '0'))

Note that this leads to a much simpler solution in any language. 请注意,这可以在任何语言中提供更简单的解决方案。 All you need is a function to convert from base ten to base two. 您所需要的只是将基数从基数转换为基数2的函数。 Then, for each number from 0 to 2 ** n , you convert it to base 2 and print or store the conversion. 然后,对于从02 ** n每个数字,将其转换为基数2并打印或存储转换。

To convert x to base two, divide it by 2 until it reaches 0 and keep track of the remainders. 要将x转换为基数2,请将其除以2直到达到0并跟踪余数。 The list of remainders, in reverse order, is x in base 2: 剩余的列表以相反的顺序在基数2中为x

x = 13
13 / 2 = 6 remainder 1
 6 / 2 = 3 remainder 0
 3 / 2 = 1 remainder 1
 1 / 2 = 0 remainder 1

=> 13 in base 2 = 1101
import itertools

#Possible characters
n = [0, 1]
#Asking required length
l = int(input("length: "))
#Putting all possibilities in list
r = list(itertools.product(n, repeat=l))

print(r)

Python always has a few handy libraries or functions to make complex things easy, and to make long taking work short. Python总是有一些方便的库或函数来简化复杂的事情,并且可以缩短工作时间。

In itertools.product() your first parameter should be an array of the characters of which you want all possibilities, and the second after the repeat-keyword is the length of the results. itertools.product()您的第一个参数应该是您想要所有可能性的字符数组,而repeat-keyword之后的第二个参数应该是结果的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何生成给定长度的所有可能组合 - How to generate all possible combinations of given length 如何生成Python中给定长度的所有配置 - How to generate all configurations of a given length in Python 生成零和b的所有可能组合 - Generating all possible combinations of a zeros and b ones 如何在Python中枚举所有可能的零和0的N长组合? - How can I enumerate all the possible N-long combinations of zeros and ones in Python? 递归算法,用于在Python中生成列表长度k的所有排列 - Recursive Algorithm to generate all permutations of length k of a list in Python Python:根据 arrays 的 1 和 0 计算结构的面积和周长 - Python: Compute the area and perimeter of structures from arrays of ones and zeros 生成所有可能的n维k * k *…* k数组,每个数组沿轴均带有一线 - Generate all possible n-dimensional k*k*…*k-arrays each with line of ones along axis 通过比较python中两个不同的整数列表,生成一个零和一个列表 - Generate a list of zeros and ones by comparing two different lists of integers in python 蟒蛇:使所有可能的字母达到给定的长度 - python: getting all possible alphabet up to given length 在 Python 中生成总和为 S 的所有可能的长度 N 列表 - Generate all possible lists of length N that sum to S in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM