Why adding multiple 'nan' in python dictionary giving multiple entries?

前端 未结 3 1692
既然无缘
既然无缘 2021-01-18 11:23

Example problem:

import numpy as np
dc = dict()
dc[np.float(\'nan\')] = 100
dc[np.float(\'nan\')] = 200

It is creating multiple entries for

3条回答
  •  甜味超标
    2021-01-18 11:32

    For historical reasons explained here, np.float('nan') == np.float('nan') is False. The rule is just that you can't have two dictionary keys which are equal to each other - so you can have two keys equal to np.float('nan').

    Of course, this behavior is counterintuitive and surprising - so you should avoid using np.float('nan') as a key.

提交回复
热议问题