• <tfoot id='gQr5u'></tfoot>

  • <legend id='gQr5u'><style id='gQr5u'><dir id='gQr5u'><q id='gQr5u'></q></dir></style></legend>
    <i id='gQr5u'><tr id='gQr5u'><dt id='gQr5u'><q id='gQr5u'><span id='gQr5u'><b id='gQr5u'><form id='gQr5u'><ins id='gQr5u'></ins><ul id='gQr5u'></ul><sub id='gQr5u'></sub></form><legend id='gQr5u'></legend><bdo id='gQr5u'><pre id='gQr5u'><center id='gQr5u'></center></pre></bdo></b><th id='gQr5u'></th></span></q></dt></tr></i><div id='gQr5u'><tfoot id='gQr5u'></tfoot><dl id='gQr5u'><fieldset id='gQr5u'></fieldset></dl></div>
      <bdo id='gQr5u'></bdo><ul id='gQr5u'></ul>

      <small id='gQr5u'></small><noframes id='gQr5u'>

      1. True + True = 2.优雅地执行布尔运算?

        True + True = 2. Elegantly perform boolean arithmetic?(True + True = 2.优雅地执行布尔运算?)

          <tbody id='ML2XL'></tbody>
          • <bdo id='ML2XL'></bdo><ul id='ML2XL'></ul>

            • <small id='ML2XL'></small><noframes id='ML2XL'>

              <tfoot id='ML2XL'></tfoot>
              <legend id='ML2XL'><style id='ML2XL'><dir id='ML2XL'><q id='ML2XL'></q></dir></style></legend>
              <i id='ML2XL'><tr id='ML2XL'><dt id='ML2XL'><q id='ML2XL'><span id='ML2XL'><b id='ML2XL'><form id='ML2XL'><ins id='ML2XL'></ins><ul id='ML2XL'></ul><sub id='ML2XL'></sub></form><legend id='ML2XL'></legend><bdo id='ML2XL'><pre id='ML2XL'><center id='ML2XL'></center></pre></bdo></b><th id='ML2XL'></th></span></q></dt></tr></i><div id='ML2XL'><tfoot id='ML2XL'></tfoot><dl id='ML2XL'><fieldset id='ML2XL'></fieldset></dl></div>
                1. 本文介绍了True + True = 2.优雅地执行布尔运算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有代表 SAT 论坛的嵌套真值列表,如下所示:

                  I have nested lists of truth values representing SAT forumlas, like this:

                  [[[0, True, False], [0, True, False], [0, True, 1]], [[0, True, True], [2, True, True], [3, False, True]], [[1, False, False], [1, False, False], [3, False, True]]]
                  

                  代表

                  ([x0=0] + [x0=0] + [x0=1]) * ([x0=1] + [x1=1] + [-x2=1]) * ([-x3=0] + [-x3=0] + [-x2=1])
                  

                  我想计算整个公式的真值.第一步是将每个子句中文字的真值相加.

                  I would like to calculate the truth value of the whole formula. First step would be adding up the truth values of the literals in each clause.

                  像这样:

                  clause_truth_value = None
                  
                  for literal in clause:
                      # multiply polarity of literal with its value
                      # sum over all literals
                      clause_truth_value += literal[1]*literal[2]
                  

                  如果clause_truth_value求和后为True,则子句整体为真.

                  if clause_truth_value is True after the summation, the clause is true as a whole.

                  但我没有得到预期的结果:

                  But I am not getting what I expected:

                  True + True = 2 不符合预期

                  True * True = 1 符合预期

                  False + False = 0 符合预期

                  False * False = 0 符合预期

                  所以... True 只是 1 而 False 是 0... 这太糟糕了,我预计算术运算符会为布尔代数重载.有没有一种优雅的方法可以用布尔变量进行布尔运算?

                  so... True is simply 1 and False is 0... that sucks, I expected the arithmetic operators to be overloaded for the boolean algebra. Is there an elegant way to do do boolean arithmetic with boolean variables?

                  推荐答案

                  在 Python 中,True == 1False == 0,为 TrueFalsebool 类型,它是 int 的子类型.当您使用运算符 + 时,它会隐式添加 TrueFalse 的整数值.

                  In Python, True == 1 and False == 0, as True and False are type bool, which is a subtype of int. When you use the operator +, it is implicitly adding the integer values of True and False.

                  int(True)
                  # 1
                  
                  int(False)
                  # 0
                  

                  您真正想要的是将 TrueFalse 视为二进制数.

                  What you really want is to treat True and False as binary numbers.

                  int(False & False)
                  # 0
                  
                  int(True & False)
                  # 0
                  
                  int(True & True)
                  # 1
                  


                  来自 Python 中的位运算符:

                  x &是的

                  执行按位与".输出的每一位为 1,如果x与y的对应位为1,否则为0.

                  Does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.

                  x |是的

                  执行按位或".如果对应的位为 0,则输出的每一位为 0x AND of y 为 0,否则为 1.

                  Does a "bitwise or". Each bit of the output is 0 if the corresponding bit of x AND of y is 0, otherwise it's 1.

                  这篇关于True + True = 2.优雅地执行布尔运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                    <small id='LnEJ1'></small><noframes id='LnEJ1'>

                  1. <tfoot id='LnEJ1'></tfoot>
                    • <legend id='LnEJ1'><style id='LnEJ1'><dir id='LnEJ1'><q id='LnEJ1'></q></dir></style></legend>
                          <bdo id='LnEJ1'></bdo><ul id='LnEJ1'></ul>
                              <tbody id='LnEJ1'></tbody>
                            <i id='LnEJ1'><tr id='LnEJ1'><dt id='LnEJ1'><q id='LnEJ1'><span id='LnEJ1'><b id='LnEJ1'><form id='LnEJ1'><ins id='LnEJ1'></ins><ul id='LnEJ1'></ul><sub id='LnEJ1'></sub></form><legend id='LnEJ1'></legend><bdo id='LnEJ1'><pre id='LnEJ1'><center id='LnEJ1'></center></pre></bdo></b><th id='LnEJ1'></th></span></q></dt></tr></i><div id='LnEJ1'><tfoot id='LnEJ1'></tfoot><dl id='LnEJ1'><fieldset id='LnEJ1'></fieldset></dl></div>