gpt4 book ai didi

python - 比较数组并添加到不同的元素?

转载 作者:行者123 更新时间:2023-11-28 22:28:02 25 4
gpt4 key购买 nike

z = np.array([1, 2, 3, 4]) 
x = np.array([4, 2, 3, 5])
n = 1

我想比较这两个数组的元素,我只想将 n 添加到 z 中与 x 中不同的那些元素。

答案应该是:

z = [2, 2, 3, 5]

最佳答案

另一种使用 np.where 的解决方案

#np.where checks if the condition is met, if yes set the value to z, otherwise z+n.
np.where(z==x, z,z+n)
Out[1257]: array([2, 2, 3, 5])

关于python - 比较数组并添加到不同的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43792778/

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