-1

Say I have a function like so:

def function(a,b):
    for x in np.nditer(a):
        dAB = x-b
        pred = np.ceil([dAB+b*2])
        print(pred)

and

array1 = np.array([1,2,3,4,5])
array2 = np.array([4,5,6,7,8])

My output is:

function(array1,array2)

[[5. 6. 7. 8. 9.]]
[[ 6.  7.  8.  9. 10.]]
[[ 7.  8.  9. 10. 11.]]
[[ 8.  9. 10. 11. 12.]]
[[ 9. 10. 11. 12. 13.]]

How would I get an output like:

function(array1,array2)

array([5,6,7,8,9,10,11,12,13])

What I'd like is to take all the unique values across the arrays and put it into one array.

AMC
  • 2,642
  • 7
  • 13
  • 35
JeffGray
  • 23
  • 3
  • Can you clarify what exactly the issue is? – AMC Mar 22 '20 at 23:19
  • Does this answer your question? [Removing duplicates in lists](https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists) – AMC Mar 22 '20 at 23:20

3 Answers3

1

Just use a set:

import numpy as np

def function(a,b):
    res = set()
    for x in np.nditer(a):
        dAB = x-b
        pred = np.ceil(dAB+b*2)
        res.update(pred)
    return np.array(res)

Ex.

>>> array1 = np.array([1,2,3,4,5])
>>> array2 = np.array([4,5,6,7,8])

>>> print(function(array1,array2))
{5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
1

you could use np.unique:

def function(a,b):
    return np.unique([np.ceil([(x-b)+b*2]) for x in np.nditer(a)])

output:

array([ 5.,  6.,  7.,  8.,  9., 10., 11., 12., 13.])
kederrac
  • 16,819
  • 6
  • 32
  • 55
1

You could compare array1 and array2 directly with:

In [249]: np.ceil((array1[:,None]-array2)+array2*2)                                                                  
Out[249]: 
array([[ 5.,  6.,  7.,  8.,  9.],
       [ 6.,  7.,  8.,  9., 10.],
       [ 7.,  8.,  9., 10., 11.],
       [ 8.,  9., 10., 11., 12.],
       [ 9., 10., 11., 12., 13.]])

and get the unique values from that:

In [250]: np.unique(np.ceil((array1[:,None]-array2)+array2*2))                                                       
Out[250]: array([ 5.,  6.,  7.,  8.,  9., 10., 11., 12., 13.])
hpaulj
  • 221,503
  • 14
  • 230
  • 353