I need to use Position
to obtain positions of entries in list of lists that do not contain a certain pattern.
The problem is that Position
wants the pattern to find. Not the pattern to avoid.
Also Position
does not take in a pure function, which one can use to do the checking and return True or False.
I do not know how to tell Position to look for entries that do not match a given pattern. Here is an example
lis = {{Tan[x], Exp[x]}, {3, x + Sin[x], 7}, {5, Cos[x], 99}}
I want to find positions of entries that does not contain the pattern Sin[_]
It is easy to find the position which contains the pattern
Position[lis, Sin[_]]
(* {{2, 2, 2}} *)
But how to do the opposite? FreeQ
does not work. The thing that goes at the end has to be a pattern.
I am not looking for other functions such as Select and Cases. I need to use Position in order to obtain the actual positions to use with Extract for example.
I looked at FunctionRepository/resources/SelectPositions and even though this does allow one to use a function, it does not return Positions in same format Position
command does.
Here is an example. Lets find entries with Sin[_]
in it
lis = {{Tan[x], Exp[x]}, {3, x + Sin[x], 7}, {5, Cos[x], 99}}
foo = Not[FreeQ[#, Sin[_]]] &;
p = ResourceFunction["SelectPositions"][lis, foo]
(* {{2}} *)
Compare to
lis = {{Tan[x], Exp[x]}, {3, x + Sin[x], 7}, {5, Cos[x], 99}}
Position[lis, Sin[_]]
(* {{2, 2, 2}} *)
The question again is: I would like to use Position
to specify not the pattern to find, but the pattern to avoid.
So it will return all those positions which do not have this pattern. For the above example those positions will be by inspection (assuming I did it right)
{{1,1},{1,2}, {2,1},{2,2,1},{2,3}, {3,1},{3,2},{3,3} }
Any suggestions?
V 13.2
pos = Position[lis, Except[Sin[_]], {2}, Heads -> False]
? $\endgroup$Except[pattern]
. This looks promissing. I do not know why it returns{{1, 1}, {1, 2}, {2, 1}, {2, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}}
instead of what I was expecting{{1,1},{1,2}, {2,1},{2,2,1},{2,3}, {3,1},{3,2},{3,3} }
but this is not important now, will look at it more. But I think this should work if you like to make it an answer. I think help for Position should had an example usingExcept
! $\endgroup$Position[lis, _?(Head[#] =!= Sin &)]
orPosition[lis, _?(FreeQ[#, Sin] &)]
$\endgroup$Sin
, but again, this was just an example. Except allows one to use a pattern inside, and I just did not know about it. $\endgroup$PatternTest
including (as shown) those that contain pure functions. $\endgroup$