Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个 numpy 数组
A = [[1 2 3] [2 3 3] [1 2 3]]
和另一个数组
B = [[3 2 3] [1 2 3] [4 6 3]]
和一组真值:
C = [[1 4 3] [8 7 3] [4 10 3]]
现在我想创建一个数组 D,其元素来自 A 或 B,条件是数组 C 中每个元素的最接近值。
有什么pythonic方法可以做到这一点吗?现在我正在使用循环
>>> K = abs(A - C) < abs(B - C) # create array of bool [[True, False, False], [True, True, False], [False, False, False]] >>> D = where(K, A, B) # get elements of A and B respectively