I want to make a dictionary from 2 numpy arrays, arr1
as keys of dictionary and arr2
as values of dictionary. The problem I encountered is the fact that I don't want to simply add values to the keys in that order, but to add a specific value if a condition to a key, element of arr1
is fullfiled. For that, I have another numpy array arr3
with one more element than arr1
and arr2
and if a key is between actual index and next element of arr3
, [i,i 1)
the value with corresponding index to the actual one will be the value paired to the actual key.
For example:
dict = {}
arr1 = np.array([0,9])
arr2 = np.array([0,5])
arr3 = np.array([0,5,10])
..
the dict should be {0:0,9:5}
CodePudding user response:
boundsWithKeyValues = zip(arr3[:-1], arr3[1:], arr1, arr2)
result = {k: v for kMin, kMax, k, v in boundsWithKeyValues if kMin <= k <= kMax}
CodePudding user response:
This would be one option. Perhaps it could be done with less code.
import numpy as np
dict_ = {}
arr1 = np.array([0,9])
arr2 = np.array([0,5])
arr3 = np.array([0,5,10])
for i,(k,v) in enumerate(zip(arr1, arr2)):
if arr3[i] <= arr1[i] and arr1[i] < arr3[i 1]:
dict_[k] = v