6

我得到了很好的帮助来检查字典键是否有空值。但我想知道pythonand之间是否有区别?&我认为它们应该是相似的?

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]}
4

5 回答 5

9

and是一个逻辑运算符,用于比较两个值,IE:

> 2 > 1 and 2 > 3
True

&是一个按位运算符,用于执行按位与运算:

> 255 & 1
1

更新

对于集合操作&操作符等价于intersection()操作,并使用 s 和 t 共有的元素创建一个新集合:

>>> a = set([1, 2, 3])
>>> b = set([3, 4, 5])
>>> a & b
set([3])

and仍然只是一个逻辑比较函数,并且会将set参数视为非假值。如果两个参数都不是,它也将返回最后一个值False

>>> a and b
set([3, 4, 5])
>>> a and b and True
True
>>> False and a and b and True
False

对于它的价值,还请注意,根据Dictionary view objects的 python 文档,返回的对象dict1.viewkeys()是一个“set-like”的视图对象:

和返回的对象dict.viewkeys()是视图对象。它们提供字典条目的动态视图,这意味着当字典更改时,视图会反映这些更改。dict.viewvalues()dict.viewitems()

...

dictview & other

将 dictview 和另一个对象的交集作为新集合返回。

...

于 2013-05-22T15:43:40.880 回答
7
  • and是合乎逻辑的
  • &是按位和

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) == Trueand&

set文档)

于 2013-05-22T15:42:11.483 回答
3

Yesand是逻辑 and 而 while&是按位与。见例子 -

>>> 1 and 2
2
>>> 1 & 2
0

第一个结果是由于短路。Python 测试 1 并发现它为真并返回 2。但是,第二部分执行 01 (Binary 1) & 10 (Binary 2) 因此评估为 00 (1 & 0, 0 &1) ,即 0。

于 2013-05-22T15:42:18.777 回答
1
>>> 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                            |
+-------------------------------------------------+---------------------------------------+
于 2013-05-22T15:45:13.780 回答
1

&是按位与运算符,and是布尔逻辑运算符。它们是完全不同的,不要混淆它们!例如:

7 & 3
=> 3

True and False
=> False
于 2013-05-22T15:42:51.403 回答