Python's and Numpy's nan and set

后端 未结 1 1745
予麋鹿
予麋鹿 2020-12-04 01:45

I ran into an unpredicted behavior with Python\'s Numpy, set and NaN (not-a-number):

>>> set([np.float64(\'nan\'), np.float64(\'nan\')])
set([nan, n         


        
相关标签:
1条回答
  • 2020-12-04 02:22

    One of the properties of NAN is that NAN != NAN, unlike all other numbers. However, the implementation of set first checks to see if id(x) matches the existing member at a hash index before it tries to insert a new one. If you have two objects with different ids that both have the value NAN, you'll get two entries in the set. If they both have the same id, they collapse into a single entry.

    As pointed out by others, np.nan is a single object that will always have the same id.

    0 讨论(0)
提交回复
热议问题