Is there a better way to compare variables if they can be NaN? Surely there must be something built-in for this, right?
NaN = float("NaN")
def remove(obj, value_1):
for key in reversed([
i for i, value_2 in
enumerate(obj) if
value_1 != value_1 and
value_2 != value_2 or
value_1 == value_2]):
del obj[key]
return obj
def test(value_1, value_2):
assert (
value_1 != value_1 and
value_2 != value_2 or
value_1 == value_2)
print(remove([0, NaN], NaN))
test(NaN, NaN)
Output:
[0]
# No assertion error
I'm writing a JSON patcher and I want to be be able to remove NaN values from a list with an operation or raise an error if a value is not equal to NaN, I don't think the normal behaviour is particularly useful here. The full code is a bit too much to share here I'm afraid.