4
$\begingroup$

I have a the following list:

list = {
        {{0,1112},{1600,1110}}, {{0,693},{600,691}}, {{0,134},{1600,132}}, 
        {{1,188},{600,188}}, {{0,413},{1600,411}}, {{0,49},{600, 49}}
       }

I want to select all elements where list[[#, 2, 1]] == 600.

The resulting list should be:

{ {{0,693},{600,691}}, {{1,188},{600,188}}, {{0,49},{600,49}} }

I tried the following:

Select[list, list[[#, 2, 1]] == 600 &]

but I get errors:

Part::pkspec1: The expression {{0,1112},{1600,1110}} cannot be used as a part specification. ...

$\endgroup$

closed as off-topic by LCarvalho, Itai Seggev, Öskå, b3m2a1, garej Aug 17 '17 at 5:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – LCarvalho, Itai Seggev, Öskå, b3m2a1, garej
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 9
    $\begingroup$ Select[list, #[[2,1]]==600&] $\endgroup$ – user1066 Aug 9 '17 at 11:10
6
$\begingroup$
Cases[list, x : {_, {600, _}} :> x]

{{{0, 693}, {600, 691}}, {{1, 188}, {600, 188}}, {{0, 49}, {600, 49}}}

$\endgroup$
7
$\begingroup$

I'd probably go for @tomd's Select or @eldo's Cases, to be honest. But for completeness I thought I should add Pick:

Pick[#, #[[2, 1]] == 600 & /@ #] & @ list

(Edit: Or as @JM points out, a cleaner implementation is

Pick[#, #[[All, 2, 1]], 600] & @ list

)

Or you could do something like

If[#[[2, 1]] == 600, #, Nothing] & /@ list

(because who knows, someday you might need it). Both of which give

{{{0, 693}, {600, 691}}, {{1, 188}, {600, 188}}, {{0, 49}, {600, 49}}}

$\endgroup$
  • 4
    $\begingroup$ Alternatively: Pick[#, #[[All, 2, 1]], 600] & @ list $\endgroup$ – J. M. will be back soon Aug 9 '17 at 13:06

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