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

      • <bdo id='tFnFf'></bdo><ul id='tFnFf'></ul>

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

      <tfoot id='tFnFf'></tfoot>

        <legend id='tFnFf'><style id='tFnFf'><dir id='tFnFf'><q id='tFnFf'></q></dir></style></legend>
      1. 两个布尔列表上的 Python AND 运算符 - 如何?

        Python AND operator on two boolean lists - how?(两个布尔列表上的 Python AND 运算符 - 如何?)
      2. <small id='u2YN6'></small><noframes id='u2YN6'>

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

              <bdo id='u2YN6'></bdo><ul id='u2YN6'></ul>
                    <tbody id='u2YN6'></tbody>
                1. 本文介绍了两个布尔列表上的 Python AND 运算符 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个布尔列表,例如,

                  I have two boolean lists, e.g.,

                  x=[True,True,False,False]
                  y=[True,False,True,False]
                  

                  我想将这些列表与预期的输出相结合:

                  I want to AND these lists together, with the expected output:

                  xy=[True,False,False,False]
                  

                  我认为表达式 x and y 会起作用,但后来发现它不起作用:事实上,(x and y) != (y and x)

                  I thought that expression x and y would work, but came to discover that it does not: in fact, (x and y) != (y and x)

                  x 和 y 的输出:[True,False,True,False]

                  y 和 x 的输出:[True,True,False,False]

                  使用列表推导确实有正确的输出.呼!

                  Using list comprehension does have correct output. Whew!

                  xy = [x[i] and y[i] for i in range(len(x)]
                  

                  请注意,我找不到任何参考资料告诉我 AND 运算符可以像我尝试使用 x 和 y 一样工作.但是在 Python 中进行尝试很容易.有人可以向我解释 x 和 y 发生了什么吗?

                  Mind you I could not find any reference that told me the AND operator would work as I tried with x and y. But it's easy to try things in Python. Can someone explain to me what is happening with x and y?

                  这是一个简单的测试程序:

                  And here is a simple test program:

                  import random
                  random.seed()
                  n = 10
                  x = [random.random() > 0.5 for i in range(n)]
                  y = [random.random() > 0.5 for i in range(n)]
                  # Next two methods look sensible, but do not work
                  a = x and y
                  z = y and x
                  # Next: apparently only the list comprehension method is correct
                  xy = [x[i] and y[i] for i in range(n)]
                  print 'x        : %s'%str(x)
                  print 'y        : %s'%str(y)
                  print 'x and y  : %s'%str(a)
                  print 'y and x  : %s'%str(z)
                  print '[x and y]: %s'%str(xy)
                  

                  推荐答案

                  and 只是根据它们的真值返回第一个或第二个操作数.如果第一个操作数为假,则返回,否则返回另一个操作数.

                  and simply returns either the first or the second operand, based on their truth value. If the first operand is considered false, it is returned, otherwise the other operand is returned.

                  列表在非空时被认为是true,因此两个列表都被认为是true.它们的内容在这里没有作用.

                  Lists are considered true when not empty, so both lists are considered true. Their contents don't play a role here.

                  因为两个列表都不为空,x 和 y 只是简单地返回第二个列表对象;只有当 x 为空时才会返回它:

                  Because both lists are not empty, x and y simply returns the second list object; only if x was empty would it be returned instead:

                  >>> [True, False] and ['foo', 'bar']
                  ['foo', 'bar']
                  >>> [] and ['foo', 'bar']
                  []
                  

                  请参阅 真值测试Python 文档中的部分:

                  See the Truth value testing section in the Python documentation:

                  可以测试任何对象的真值,用于 ifwhile 条件或作为以下布尔运算的操作数.以下值被认为是错误的:

                  Any object can be tested for truth value, for use in an if or while condition or as operand of the Boolean operations below. The following values are considered false:

                  [...]

                  • 任何空序列,例如,''()[].

                  [...]

                  所有其他值都被认为是真——因此许多类型的对象总是真.

                  All other values are considered true — so objects of many types are always true.

                  (强调我的)和 布尔运算部分就在它下面:

                  (emphasis mine), and the Boolean operations section right below that:

                  x 和 y
                  如果 x 为假,则 x,否则 y

                  这是一个短路操作符,所以它只在第一个参数为 True 时才计算第二个参数.

                  This is a short-circuit operator, so it only evaluates the second argument if the first one is True.

                  您确实需要明确地测试列表中包含 的值.正如您所发现的,您可以通过列表理解来做到这一点.您可以使用 zip() 函数重写它 将值配对:

                  You indeed need to test the values contained in the lists explicitly. You can do so with a list comprehension, as you discovered. You can rewrite it with the zip() function to pair up the values:

                  [a and b for a, b in zip(x, y)]
                  

                  这篇关于两个布尔列表上的 Python AND 运算符 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 与否?)

                2. <i id='rrbdB'><tr id='rrbdB'><dt id='rrbdB'><q id='rrbdB'><span id='rrbdB'><b id='rrbdB'><form id='rrbdB'><ins id='rrbdB'></ins><ul id='rrbdB'></ul><sub id='rrbdB'></sub></form><legend id='rrbdB'></legend><bdo id='rrbdB'><pre id='rrbdB'><center id='rrbdB'></center></pre></bdo></b><th id='rrbdB'></th></span></q></dt></tr></i><div id='rrbdB'><tfoot id='rrbdB'></tfoot><dl id='rrbdB'><fieldset id='rrbdB'></fieldset></dl></div>
                    <bdo id='rrbdB'></bdo><ul id='rrbdB'></ul>
                          <tfoot id='rrbdB'></tfoot>
                            <tbody id='rrbdB'></tbody>

                          • <legend id='rrbdB'><style id='rrbdB'><dir id='rrbdB'><q id='rrbdB'></q></dir></style></legend>

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