简体   繁体   中英

Numpy - for each element X in array1 find index of first element greater than X in array2

I have two numpy arrays x and y

x = numpy.array([1,2,3,4,5])
y = numpy.array([1,3,5,7])

z = func(x,y) # z = [2,2,3,3,4]

is there a function func that will return the above. Eg the index of the first element in y that is greater than 1 (first element in x) is 2 so z[0]=2

Looking for a one liner vectorized way to do this if it exists. Something that is agnostic to the actual comparison function (less than, greater than, leq, geq, etc)

  1. Create array1 with 15 even numbers.
  2. Create array2 with 15 odd numbers.
  3. Exchange array1, array2.
  4. Addition array1, array2 and put the result in array3.
  5. Print all prime number where there are array3.
  6. Apply AND, OR, XOR operations using first element in array1 with first element in array2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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