I am using Mathematica7. I have a list of lists (creatively named list
in the following sample code) containing data. I would like to find the positions of the elements that satisfy a particular criterion. However, Position
takes a pattern as input, whereas Select
takes a criterion as input.
Suppose I want to find the positions of all lists (i.e., sublists) whose first element is 1
. The following code works (or so it seems), but it generates error messages as well:
list = {{1, "A"}, {1, "B"}, {2, "C"}};
Position[list, _?(#[[1]] == 1 &)]
The output looks correct: {{1},{2}}
But I also get the following error messages, which in a long computation seem to slow down Position
:
Part::partd : Part specification List[[1]] is longer than depth of object. Part::partd : Part specification List[[1]] is longer than depth of object. Part::partd : Part specification [[1]][[1]] is longer than depth of object. General::stop : Further output of Part::partd will be suppressed during this calculation.
Does my code indeed work properly? If so, why do I get the error messages? If not, how can I improve my code?