[英]Is there a difference between 'and' and '&' with respect to python sets?
如果字典键有空值,我对问题检查有很好的帮助。 但我想知道如果有之间的差异and
和&
在Python? 我认为它们应该相似吗?
dict1 ={"city":"","name":"yass","region":"","zipcode":"",
"phone":"","address":"","tehsil":"", "planet":"mars"}
whitelist = {"name", "phone", "zipcode", "region", "city",
"munic", "address", "subarea"}
result = {k: dict1[k] for k in dict1.viewkeys() & whitelist if dict1[k]}
and
是一个逻辑运算符,用于比较两个值,IE:
> 2 > 1 and 2 > 3
True
&
是一个按位运算符,用于执行按位AND运算:
> 255 & 1
1
更新
对于set操作 , &
运算符等同于intersection()
操作,并创建一个包含s和t共有元素的新集合:
>>> a = set([1, 2, 3])
>>> b = set([3, 4, 5])
>>> a & b
set([3])
and
仍然只是一个逻辑比较函数,并将set
参数视为非false值。 如果两个参数都不为False
它也将返回最后一个值:
>>> a and b
set([3, 4, 5])
>>> a and b and True
True
>>> False and a and b and True
False
值得注意的是,根据Dictionary视图对象的python文档, dict1.viewkeys()
返回的对象是一个“类似set”的视图对象:
dict.viewkeys()
,dict.viewvalues()
和dict.viewitems()
返回的对象是视图对象。 它们提供字典条目的动态视图,这意味着当字典更改时,视图会反映这些更改。...
dictview & other
将dictview和另一个对象的交集作为新集返回。
...
and
是合乎逻辑的 &
是按位和 如果两个值都计算为true and
返回第二个值。
套装&
交叉。
如果你这样做:
In [25]: a = {1, 2, 3}
In [26]: b = {3, 4, 5}
In [27]: a and b
Out[27]: set([3, 4, 5])
In [28]: a & b
Out[28]: set([3])
这是因为bool(a) == True
和bool(b) == True
所以and
返回第二组。 &
返回集合的交集。
是的and
是合乎逻辑的,而&
是一个按位和。 见例子 -
>>> 1 and 2
2
>>> 1 & 2
0
第一个结果是由于短路。 Python测试1并发现它为真并返回2.但是,第二部分执行01(二进制1)和10(二进制2)因此评估为00(1&0,0和1),即0。
&
是逐位和操作员, and
是布尔逻辑运算器。 他们是完全不同的,不要混淆他们! 例如:
7 & 3
=> 3
True and False
=> False
>>> help('&')
+-------------------------------------------------+---------------------------------------+
| Operator | Description |
+=================================================+=======================================+
| ``lambda`` | Lambda expression |
+-------------------------------------------------+---------------------------------------+
| ``if`` -- ``else`` | Conditional expression |
+-------------------------------------------------+---------------------------------------+
| ``or`` | Boolean OR |
+-------------------------------------------------+---------------------------------------+
| ``and`` | Boolean AND |
+-------------------------------------------------+---------------------------------------+
| ``not`` ``x`` | Boolean NOT |
+-------------------------------------------------+---------------------------------------+
| ``in``, ``not in``, ``is``, ``is not``, ``<``, | Comparisons, including membership |
| ``<=``, ``>``, ``>=``, ``<>``, ``!=``, ``==`` | tests and identity tests, |
+-------------------------------------------------+---------------------------------------+
| ``|`` | Bitwise OR |
+-------------------------------------------------+---------------------------------------+
| ``^`` | Bitwise XOR |
+-------------------------------------------------+---------------------------------------+
| ``&`` | Bitwise AND |
+-------------------------------------------------+---------------------------------------+
| ``<<``, ``>>`` | Shifts |
+-------------------------------------------------+---------------------------------------+
| ``+``, ``-`` | Addition and subtraction |
+-------------------------------------------------+---------------------------------------+
| ``*``, ``/``, ``//``, ``%`` | Multiplication, division, remainder |
| | [8] |
+-------------------------------------------------+---------------------------------------+
| ``+x``, ``-x``, ``~x`` | Positive, negative, bitwise NOT |
+-------------------------------------------------+---------------------------------------+
| ``**`` | Exponentiation [9] |
+-------------------------------------------------+---------------------------------------+
| ``x[index]``, ``x[index:index]``, | Subscription, slicing, call, |
| ``x(arguments...)``, ``x.attribute`` | attribute reference |
+-------------------------------------------------+---------------------------------------+
| ``(expressions...)``, ``[expressions...]``, | Binding or tuple display, list |
| ``{key: value...}``, ```expressions...``` | display, dictionary display, string |
| | conversion |
+-------------------------------------------------+---------------------------------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.