简体   繁体   English

格式化两个 arrays 与相关 Numpy Python

[英]Formatting two arrays with a correlation Numpy Python

I am trying to write a numpy function where Numbers and value are in correlation with each other and if the element of Numbers array is smaller than the value element than it will return the number 0 if the case is otherwise the returned number will be 1. If the numbers are equal it will return 2. So the first element in Numbers and value is 3 and 2, since the Numbers value is greater than value s number it will return 1. I would like to avoid for loops and list comprehensions if possible.我正在尝试编写一个 numpy function ,其中Numbersvalue彼此相关,如果Numbers数组的元素小于value元素,它将返回数字 0,否则返回的数字将为 1。如果数字相等,它将返回 2。因此Numbersvalue中的第一个元素是 3 和 2,因为Numbers值大于value s 数字,它将返回 1。如果可能,我想避免 for 循环和列表推导.

import numpy as np

Numbers = np.array([3,8,10,14,28,42])
value =   np.array([2,4,12,13,30,42])

Expected Output:预期 Output:

Result = np.array([1,1,0,1,0,2])

Use numpy.select :使用numpy.select

>>> np.select(condlist=[Numbers>value, Numbers<value, Numbers==value],
              choicelist=[1, 0, 2])

array([1, 1, 0, 1, 0, 2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM