我正在尝试使用字典查找将 numpy dtypes 映射到关联的值。我观察到以下违反直觉的行为:
dtype = np.uint16
x = np.array([0, 1, 2], dtype=dtype)
assert x.dtype == dtype
d = {np.uint8: 8, np.uint16: 16, np.uint32: 32, np.float32: 32}
print(dtype in d) # prints True
print(x.dtype in d) # prints False
Run Code Online (Sandbox Code Playgroud)
使用其他 dtypes 会产生类似的结果。
所以我们有那个np.uint16 == x.dtype,但前者在字典的键中找到,而后者则没有。任何解释和/或简单的解决方法将不胜感激。