我正在尝试制作具有所有可能组合的 4x4 (16) 像素黑白图像数组。我制作了以下数组作为模板:
template = [[0,0,0,0], # start with all white pixels
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
然后我想迭代模板并将每个可能的组合的 0 更改为 1。
我尝试用 numpy 和 itertools 进行迭代,但只能得到 256 个组合,根据我的计算,应该有 32000 个(编辑:65536!不知道那里发生了什么......)。有哪位有疯狂技能的人可以帮助我吗?
正如你所说,你可以使用itertools
模块来做到这一点,特别是product
功能:
import itertools
import numpy as np
# generate all the combinations as string tuples of length 16
seq = itertools.product("01", repeat=16)
for s in seq:
# convert to numpy array and reshape to 4x4
arr = np.fromiter(s, np.int8).reshape(4, 4)
# do something with arr
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)