Python和运算符对两个布尔名单 - 怎么样? [英] Python AND operator on two boolean lists - how?

查看:210
本文介绍了Python和运算符对两个布尔名单 - 怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个布尔列表,如,

  X = [真,真,假,假]
Y = [真,假,真,假]

我要和这些列表一起,与预期输出:

  XY = [真,假,假,假]

我认为前pression x和y 会的工作,但来到发现,它不会:其实,(X和y)!=(y和x)

输出x和y [真,假,真,假]

输出 Y和X [真,真,假,假]

使用列表COM prehension的确实的有正确的输出。呼!

  XY = [X [i]和值Y [i]因为我在范围内(LEN(X)]

提醒你,我无法找到该告诉我任何的参考,当我与x和y尝试操作员的工作。但它很容易尝试在Python中的东西。
可以给我一个人解释什么与发生 x和y

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

 进口随机
random.seed()
N = 10
X = [random.random()> 0.5我在范围内(N)]
Y = [random.random()> 0.5我在范围内(N)]
#接下来的两个方法看起来合理,但不工作
一个= x和y
Z = Y和X
#下一页:显然只有列表中的COM prehension方法是正确的
的xy = [X [i]和值Y [i]为i的范围(N)]
打印X:%s'的%STR(X)
打印'Y:%s'的%STR(Y)
打印'x和y:%s'的%力量(一)
打印'y和x:%s的STR%(Z)
打印[x和y]:%s的STR%(XY)


解决方案

只返回第一或者第二个操作数,根据其真值。如果第一个操作数被认为是假的,则返回,否则返回其他操作。

列出被认为的真正的时候的不为空的,所以这两个名单被认为是真实的。其内容的请不要在此处发挥作用

由于这两个列表不为空, x和y 只返回第二个列表对象;只有当 X 是空的将它代替返回:

 >>> [真,假]和['富','酒吧']
['富','酒吧']
>>> []和['富','酒吧']
[]

查看真值测试的部分 Python文档中:


  

任何对象都可以为真值进行测试,在使用如果,而的条件或操作数下面的布尔运算。以下值被认为是假的:


  
  

[...]


  
  

      
  • 的空序列,例如() []

  •   

  
  

[...]


  
  

所有其他值都认为是真 - 有这么多类型的对象总是为真


(重点煤矿),以及布尔运算部分权低于:


  

x和y 结果
  如果的 X 的是假的,那么的 X 的,其他的


  
  

这是一个短路的运营商,因此,如果第一个是

只计算第二个参数

您确实需要测试的值的在列表中包含的明确。你可以用一个列表COM prehension这样做,因为你发现了。您可以通过 的zip()函数<改写/ A>,以配对值:

  [A和B,A,B拉链(X,Y)]

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]

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)

Output of x and y: [True,False,True,False]

Output of y and 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)]

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 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.

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

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']
[]

See the Truth value testing section in the Python documentation:

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:

[...]

  • any empty sequence, for example, '', (), [].

[...]

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 and y
if x is false, then x, else y

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

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和运算符对两个布尔名单 - 怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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