([False,True]和[True,True])的计算结果为[True,True] [英] ([False, True] and [True, True]) evaluates to [True, True]

查看:278
本文介绍了([False,True]和[True,True])的计算结果为[True,True]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python 3中观察到以下行为:

I have observed the following behavior in python 3:

>>> ([False, True] and [True, True])
[True, True]

>>> ([False, True] or [True, True])
[False, True]

我期望完全相反:

[False, True] and [True, True] = [False and True, True and True] = [False, True]
[False, True] or [True, True] = [False or True, True or True] = [True, True]

观察到的行为如何有意义,我如何实现所需的行为?

How does the observed behavior makes sense and how can I achieve the desired behavior?

推荐答案

您要使用numpy.标准lib python正在评估:

You want to use numpy. Standard lib python is evaluating:

bool([False, True]) and bool([True, True])

由于两者均为True,因此选择最后一个(尝试1 and 2)

Since both are True, it selects the last (try out 1 and 2)

如果您这样做:

import numpy
numpy.bitwise_and([False, True], [True, True])

您将得到想要的东西.

如果不想使用numpy,则需要编写列表理解或循环:

If you don't want to use numpy, you need to write a list comprehension or loop:

result = [(x and y) for x, y in zip([False, True], [True, True])]

这篇关于([False,True]和[True,True])的计算结果为[True,True]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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