Zanam - 1 year ago 60

Python Question

I have a smaller array as:

`A = np.array([2011, 2014, 2015, 2016, 2017])`

Aval = np.array([14, 10, 35, 40, 45])

I have another array:

`A2 = np.array([2013, 2014, 2015, 2014, 2015, 2016, 2016, 2016, 2017])`

I want to create A2val such that:

`Arval = np.array([10, 35, 10, 35, 40, 40, 40, 45])`

so, I am trying to use the values in array A to map to elements of A2 and generate an extended version of A2val

Please note 2011 is present in A, 2013 is in A2 but not in A2 and A respectively. I can use the following suggested in another thread:

`Aval[np.searchsorted(A,A2)]`

But it doesn't produce the answer I am looking for.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Here is one way:

```
>>> Aval[np.searchsorted(A, A2[np.nonzero(np.in1d(A2, A))[0]])]
array([10, 35, 10, 35, 40, 40, 40, 45])
```

Note that for getting the expected indices in default order the second array that you pass to `searchsorted()`

should be contain the common items with first array.