I have a large multi column data file, but for this question it can simplified as follows:
data = {{"a", 2000}, {"a", 2010}, {"b", 1999}, {"b", 2004}, {"b",
2006}, {"c", 2012}, {"c", 2014}};
I then have a list of items for which I want to extract the year value from data, e.g:
selectedList = {"b", "c"};
I can do it by using Select[] and then iterating through the selectedList:
Table[
Select[data, #[[1]] == selectedList[[i]] &][[All, 2]],
{i, 1, Length[selectedList]} ]
However I want to use Map, which should be faster than Table. I can do this:
func[dat_, x_] := Select[dat, #[[1]] == x &][[All, 2]]
and then :
func[data, #] & /@ selectedList
I am looking for a more elegant way to do this in one step, preferably mapping Select directly onto selectedList
Cases[data, {#, x_} :> x] & /@ selectedList
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With