3
$\begingroup$

I want to select the elements of a list like:

list ={{"AED", 0.27224}, {"AUD", 0.7029}, {"BRL", 0.25138}, {"CAD", 
  0.7568}, {"CHF", 1.0311}, {"CLP", 0.00144}, {"CNH", 
  0.15714}, {"CNY", 0.15733}, {"COP", 0.03304}, {"CZK", 
  0.04148}, {"DKK", 0.1511}, {"EGP", 0.12771}, {"EUR", 
  1.1272}, {"GBP", 1.52}, {"HKD", 0.12902}, {"HUF", 0.3612}, {"IDR", 
  0.06828}, {"ILS", 0.25478}, {"INR", 1.5297}, {"JPY", 
  0.8388}, {"KRW", 0.84796}, {"MAD", 0.10305}, {"MXN", 
  0.05943}, {"MYR", 0.22635}, {"NOK", 0.11999}, {"NZD", 
  0.6429}, {"PEN", 0.3097}, {"PHP", 0.02143}, {"PLN", 0.2651}, {"SEK",
   0.1202}, {"SGD", 0.6982}, {"THB", 0.02736}, {"TRY", 
  0.3339}, {"TWD", 0.03044}, {"USD", 1}, {"ZAR", 0.0726}}

using Select[list,#[[1]]=={"AED"|"CNH"|"MAD"|...}]

but I can't get the list of alternatives to work. Nothing is selected.

Actually, the first list is a list of lists, and there are more alternatives then I shown above.

$\endgroup$
  • 1
    $\begingroup$ Why not Select[list, MemberQ[{"AED", "CNH", "MAD"}, #[[1]]] &] ? Or Cases[list, { "AED" | "CNH" | "MAD", _}]. $\endgroup$ – b.gates.you.know.what Oct 16 '15 at 16:06
  • $\begingroup$ Or Select[list, Or @@ Thread[#[[1]] == {"AED", "CNH", "MAD"}] &] $\endgroup$ – Dr. belisarius Oct 16 '15 at 16:07
  • $\begingroup$ Oh, the @@ operator, which I think you once said is your favorite. U didn't think about that. I'll try it. $\endgroup$ – George Wolfe Oct 16 '15 at 16:13
  • $\begingroup$ @belisariusisforth the Or @@ Thread approach worked. I'll check out the other approaches too. $\endgroup$ – George Wolfe Oct 16 '15 at 16:27
12
$\begingroup$

If you want to use Alternatives[] (|), I'd say Cases[] is the more natural choice:

Cases[list, {"AED" | "CNH" | "MAD", __}]

but if you really want to use Select[], then you can consider using MatchQ[]:

Select[list, MatchQ[#[[1]], "AED" | "CNH" | "MAD"] &]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.