gpt4 book ai didi

Python/NumPy : Return a List of Smaller (LHS) + Larger Elements (RHS) out of the Two Lists

转载 作者:太空宇宙 更新时间:2023-11-04 10:25:57 24 4
gpt4 key购买 nike

大家好,我想做一些简单的事情,比较 AB 然后返回左侧列(第 0 列)中较小的元素和右侧列(第 1 列)上的较大元素。

说,

import numpy as np
A = np.array([[ 311.,360.],
[-4022.,-3973.],
[ 96.,145.],
[ 3989.,4038.]])

和,

B = np.array([[310.,460.],
[ -4018.,4013.],
[ -297.,-256.],
[ 4005.,4039.]])

我想得到:-

C = [[  310.,460.],
[-4022.,4013.],
[ -297.,145.],
[ 3989.,4039.]]

我试过 A[A>B] 但元素的位置没有按顺序排列。有解决这个问题的巧妙方法吗?感谢您的关注和帮助!

最佳答案

使用 A[A>B],您可以获得 A 的所有元素的列表,这些元素大于 B 的相应元素。改用

>>> idx1 = A[:,0]<B[:,0]
>>> idx2 = A[:,1]>B[:,1]
>>> idx = np.column_stack((idx1,idx2))
>>> np.where(idx,A,B)
array([[ 310., 460.],
[-4022., 4013.],
[ -297., 145.],
[ 3989., 4039.]])

关于Python/NumPy : Return a List of Smaller (LHS) + Larger Elements (RHS) out of the Two Lists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29275053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com