• <i id='q4oGb'><tr id='q4oGb'><dt id='q4oGb'><q id='q4oGb'><span id='q4oGb'><b id='q4oGb'><form id='q4oGb'><ins id='q4oGb'></ins><ul id='q4oGb'></ul><sub id='q4oGb'></sub></form><legend id='q4oGb'></legend><bdo id='q4oGb'><pre id='q4oGb'><center id='q4oGb'></center></pre></bdo></b><th id='q4oGb'></th></span></q></dt></tr></i><div id='q4oGb'><tfoot id='q4oGb'></tfoot><dl id='q4oGb'><fieldset id='q4oGb'></fieldset></dl></div>
    1. <tfoot id='q4oGb'></tfoot>

      <small id='q4oGb'></small><noframes id='q4oGb'>

        <bdo id='q4oGb'></bdo><ul id='q4oGb'></ul>
      <legend id='q4oGb'><style id='q4oGb'><dir id='q4oGb'><q id='q4oGb'></q></dir></style></legend>

      1. 检查容器中是否存在 NaN

        Checking for NaN presence in a container(检查容器中是否存在 NaN)
      2. <i id='3A9mc'><tr id='3A9mc'><dt id='3A9mc'><q id='3A9mc'><span id='3A9mc'><b id='3A9mc'><form id='3A9mc'><ins id='3A9mc'></ins><ul id='3A9mc'></ul><sub id='3A9mc'></sub></form><legend id='3A9mc'></legend><bdo id='3A9mc'><pre id='3A9mc'><center id='3A9mc'></center></pre></bdo></b><th id='3A9mc'></th></span></q></dt></tr></i><div id='3A9mc'><tfoot id='3A9mc'></tfoot><dl id='3A9mc'><fieldset id='3A9mc'></fieldset></dl></div>
        • <bdo id='3A9mc'></bdo><ul id='3A9mc'></ul>
              <tbody id='3A9mc'></tbody>
            <tfoot id='3A9mc'></tfoot>

              <small id='3A9mc'></small><noframes id='3A9mc'>

                <legend id='3A9mc'><style id='3A9mc'><dir id='3A9mc'><q id='3A9mc'></q></dir></style></legend>
                  本文介绍了检查容器中是否存在 NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  NaN is handled perfectly when I check for its presence in a list or a set. But I don't understand how. [UPDATE: no it's not; it is reported as present if the identical instance of NaN is found; if only non-identical instances of NaN are found, it is reported as absent.]

                  1. I thought presence in a list is tested by equality, so I expected NaN to not be found since NaN != NaN.

                  2. hash(NaN) and hash(0) are both 0. How do dictionaries and sets tell NaN and 0 apart?

                  3. Is it safe to check for NaN presence in an arbitrary container using in operator? Or is it implementation dependent?

                  My question is about Python 3.2.1; but if there are any changes existing/planned in future versions, I'd like to know that too.

                  NaN = float('nan')
                  print(NaN != NaN) # True
                  print(NaN == NaN) # False
                  
                  list_ = (1, 2, NaN)
                  print(NaN in list_) # True; works fine but how?
                  
                  set_ = {1, 2, NaN}
                  print(NaN in set_) # True; hash(NaN) is some fixed integer, so no surprise here
                  print(hash(0)) # 0
                  print(hash(NaN)) # 0
                  set_ = {1, 2, 0}
                  print(NaN in set_) # False; works fine, but how?
                  

                  Note that if I add an instance of a user-defined class to a list, and then check for containment, the instance's __eq__ method is called (if defined) - at least in CPython. That's why I assumed that list containment is tested using operator ==.

                  EDIT:

                  Per Roman's answer, it would seem that __contains__ for list, tuple, set, dict behaves in a very strange way:

                  def __contains__(self, x):
                    for element in self:
                      if x is element:
                        return True
                      if x == element:
                        return True
                    return False
                  

                  I say 'strange' because I didn't see it explained in the documentation (maybe I missed it), and I think this is something that shouldn't be left as an implementation choice.

                  Of course, one NaN object may not be identical (in the sense of id) to another NaN object. (This not really surprising; Python doesn't guarantee such identity. In fact, I never saw CPython share an instance of NaN created in different places, even though it shares an instance of a small number or a short string.) This means that testing for NaN presence in a built-in container is undefined.

                  This is very dangerous, and very subtle. Someone might run the very code I showed above, and incorrectly conclude that it's safe to test for NaN membership using in.

                  I don't think there is a perfect workaround to this issue. One, very safe approach, is to ensure that NaN's are never added to built-in containers. (It's a pain to check for that all over the code...)

                  Another alternative is watch out for cases where in might have NaN on the left side, and in such cases, test for NaN membership separately, using math.isnan(). In addition, other operations (e.g., set intersection) need to also be avoided or rewritten.

                  解决方案

                  Question #1: why is NaN found in a container when it's an identical object.

                  From the documentation:

                  For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y).

                  This is precisely what I observe with NaN, so everything is fine. Why this rule? I suspect it's because a dict/set wants to honestly report that it contains a certain object if that object is actually in it (even if __eq__() for whatever reason chooses to report that the object is not equal to itself).

                  Question #2: why is the hash value for NaN the same as for 0?

                  From the documentation:

                  Called by built-in function hash() and for operations on members of hashed collections including set, frozenset, and dict. hash() should return an integer. The only required property is that objects which compare equal have the same hash value; it is advised to somehow mix together (e.g. using exclusive or) the hash values for the components of the object that also play a part in comparison of objects.

                  Note that the requirement is only in one direction; objects that have the same hash do not have to be equal! At first I thought it's a typo, but then I realized that it's not. Hash collisions happen anyway, even with default __hash__() (see an excellent explanation here). The containers handle collisions without any problem. They do, of course, ultimately use the == operator to compare elements, hence they can easily end up with multiple values of NaN, as long as they are not identical! Try this:

                  >>> nan1 = float('nan')
                  >>> nan2 = float('nan')
                  >>> d = {}
                  >>> d[nan1] = 1
                  >>> d[nan2] = 2
                  >>> d[nan1]
                  1
                  >>> d[nan2]
                  2
                  

                  So everything works as documented. But... it's very very dangerous! How many people knew that multiple values of NaN could live alongside each other in a dict? How many people would find this easy to debug?..

                  I would recommend to make NaN an instance of a subclass of float that doesn't support hashing and hence cannot be accidentally added to a set/dict. I'll submit this to python-ideas.

                  Finally, I found a mistake in the documentation here:

                  For user-defined classes which do not define __contains__() but do define __iter__(), x in y is true if some value z with x == z is produced while iterating over y. If an exception is raised during the iteration, it is as if in raised that exception.

                  Lastly, the old-style iteration protocol is tried: if a class defines __getitem__(), x in y is true if and only if there is a non-negative integer index i such that x == y[i], and all lower integer indices do not raise IndexError exception. (If any other exception is raised, it is as if in raised that exception).

                  You may notice that there is no mention of is here, unlike with built-in containers. I was surprised by this, so I tried:

                  >>> nan1 = float('nan')
                  >>> nan2 = float('nan')
                  >>> class Cont:
                  ...   def __iter__(self):
                  ...     yield nan1
                  ...
                  >>> c = Cont()
                  >>> nan1 in c
                  True
                  >>> nan2 in c
                  False
                  

                  As you can see, the identity is checked first, before == - consistent with the built-in containers. I'll submit a report to fix the docs.

                  这篇关于检查容器中是否存在 NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  • <legend id='HbFt2'><style id='HbFt2'><dir id='HbFt2'><q id='HbFt2'></q></dir></style></legend>

                        <i id='HbFt2'><tr id='HbFt2'><dt id='HbFt2'><q id='HbFt2'><span id='HbFt2'><b id='HbFt2'><form id='HbFt2'><ins id='HbFt2'></ins><ul id='HbFt2'></ul><sub id='HbFt2'></sub></form><legend id='HbFt2'></legend><bdo id='HbFt2'><pre id='HbFt2'><center id='HbFt2'></center></pre></bdo></b><th id='HbFt2'></th></span></q></dt></tr></i><div id='HbFt2'><tfoot id='HbFt2'></tfoot><dl id='HbFt2'><fieldset id='HbFt2'></fieldset></dl></div>
                          • <bdo id='HbFt2'></bdo><ul id='HbFt2'></ul>

                              <tbody id='HbFt2'></tbody>

                            <small id='HbFt2'></small><noframes id='HbFt2'>

                            <tfoot id='HbFt2'></tfoot>