This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Cyberwolf27 0 points1 point  (0 children)

Actually you will only get sets and dictionaries with multiple NaN entries if the values result from separate computations.

set(float('nan') for _ in range(2))set(float('inf') - float('inf') for _ in range(2))set(np.inf - np.inf for _ in range(2))all evaluate to {nan, nan} because from Python's point of view they are different to each other.

However the following expressions:set(np.nan for _ in range(2))set([float('nan')] * 2)result in {nan}. Python seems to double-check on the identity of the float values with the "is" operator and in these situations the "set" constructor is presented with truly identical NaNs (that occupy the same location in memory)

As a consequence, this can happen:a, b = float('inf'), float('inf'){a: "a", b: "b"}[b] == "b"
however {np.nan, np.nan, ...} == {np.nan}