将(2d)numpy 数组的所有值异或在一起

问题描述 投票:0回答:1

如果我有一个 2d numpy 数组(数据不一定是连续的,但例如)

[[0. 1. 2. 3.]
 [4. 5. 6. 7.]
 [8. 9. 10. 11.]]

如何计算 0^1^2^3^4^5^6^7^8^9^10?

我想我可以将其展平,然后使用 np.vectorize() 但我想知道是否可以直接使用 numpy 操作?从这里我尝试了

np.bitwise_xor.reduce(a)
但它说
TypeError: No loop matching the specified signature and casting was found for ufunc bitwise_xor

python numpy vectorization xor
1个回答
1
投票

您需要将数组转换为某种整数类型

np.bitwise_xor.reduce(np.array([0.0, 1.0, 2.0, 3.0]).astype(np.int32))

-> 0

© www.soinside.com 2019 - 2024. All rights reserved.