生成真值表 [英] Generating a Truth Table

查看:150
本文介绍了生成真值表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一些值A B C,可以生成所有可能的真值的列表:
输入是[X,Y,Z]
例如该列表包含8个列表(行的表)

  [[(X,True),(Y,True ),(Z,True)],
[(X,True),(Y,True),(Z,False)] ....]

有没有提示如何做到这一点?

我想为任意数量的变量生成一个真值表。

解决方案

您需要的不是真值表,而是笛卡尔产品。
你可以使用list comprehension获得你想要的结果: [(x,y,z)| x < - [True,False],y < - [True,False],z < - [True,False]]
$ b <如果你想保留变量名,你可以使用元组: [(x,y,z)| ([X,True),(X,False)],y < - [(Y,False)],z←[ Z,True),(Z,False)]]


If I have some values A B C, is is possible to generate a list of all their possible truth values: the input is ["X", "Y", "Z"] e.g. the list contains 8 lists (rows of table)

[ [ ("X",True), ("Y",True), ("Z", True) ],
[ ("X",True), ("Y",True), ("Z", False) ],.... ]

Are there any hints on how to do this?

I would like to generate a truth table for an arbitrary number of variables.

解决方案

What you need is not a truth table but rather a cartesian product. You can obtain what you want using list comprehension: [(x,y,z) | x <- [True, False], y <- [True,False], z <- [True,False]]

If you want to keep variable names you can use tuples: [(x,y,z) | x <- [("X", True), ("X", False)], y <- [("Y", True), ("Y", False)], z <- [("Z", True),("Z", False)]]

这篇关于生成真值表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆