'和'和'&'之间有区别吗?关于python集?

时间:2013-05-22 15:40:40

标签: python set

我对问题check if dictionary key has empty value得到了很好的帮助。但是我想知道python中and&之间是否存在差异?我认为它们应该相似吗?

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

5 个答案:

答案 0 :(得分:7)

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

> 2 > 1 and 2 > 3
True

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

> 255 & 1
1

更新

对于set operations&运算符等效于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()返回的对象是一个“类似于”的视图对象:

  

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

     

...

     

dictview & other

     

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

     

...

答案 1 :(得分:5)

  • and符合逻辑且
  • &是按位和
如果两个值都计算为true,则

logical 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) == Truebool(b) == True所以and会返回第二组。 &返回集合的交集。

(set doc)

答案 2 :(得分:3)

and是合乎逻辑的,而&是按位的。见例子 -

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

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

答案 3 :(得分:1)

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

7 & 3
=> 3

True and False
=> False

答案 4 :(得分: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                            |
+-------------------------------------------------+---------------------------------------+