数组的动态循环 输出

问题描述

我想创建一个TruthTable制造商。因此,如果我有一些n变量,我将有2 ^ n行。每个语句仅包含零和一。

例如,考虑A ^ B。 A的可能值为[0,1],B的可能值为[0,1]。我要为A和B申请AND。我不想以常规循环的方式申请,因为,如果变量大于2,我应该对每种可能性进行硬编码循环。

我的要求是否有任何麻木的方法?我的意思是对两个数组的每个元素应用一个操作。

解决方法

这应该对您有用-

import numpy as np

# the resulting table need not be square
A = np.array([[0,1]])
B = np.array([[0,1,1]])
# repeat B 'row' times
rows = A.shape[1]
B = np.tile(B,(rows,1))
# transpose A to columns and perform element wise logical and operation
print A.T & B

print (A.T & B) != 0

输出

[[0 0 0]
 [0 1 1]]

[[False False False]
 [False  True  True]]

ps-我要说的是,问题的标题并没有完全抓住您问题的本质

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...