Consider the following the list and association.
list1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
asc = <|1 -> a, 4 -> b, 7 -> c, 10 -> d|>;
I would like to create an association which will have the keys same as list1
and values based on the upper key's value of asc
. For example, asc
does not have 2
key, so I would want to add an element 2 -> asc[4]
i.e., 2 -> b
and so on...
Required output is
<|1 -> a, 2 -> b, 3 -> b, 4 -> b, 5 -> c, 6 -> c, 7 -> c, 9 -> d, 10 -> d|>
The following is what I have tried.
f[n_] := If[! MemberQ[Keys[asc], n], asc[SelectFirst[Keys[asc], # > n &]], asc[n]]
Thread[list1 -> f /@ list1] // Association
Is there any elegant way to do this?
Nearest[]
type function except that it returns the upper key's value. $\endgroup$