Function Repository Resource:

SelectPositions

Source Notebook

A function like Select that returns positions instead of values

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["SelectPositions"][list,crit]

picks out all positions pi of list for which crit[elementi] is True.

ResourceFunction["SelectPositions"][list,crit,n]

picks out the first n positions for which crit[elementi] is True.

ResourceFunction["SelectPositions"][crit]

represents an operator form of ResourceFunction["SelectPositions"] that can be applied to an expression.

Details and Options

The object list can have any head, not necessarily List.
When used on an Association, ResourceFunction["SelectPositions"] picks out positions according to their values.
ResourceFunction["SelectPositions"] can be used on SparseArray objects.
ResourceFunction["SelectPositions"][crit][list] is equivalent to ResourceFunction["SelectPositions"][list,crit].

Examples

Basic Examples (4) 

Select positions that have even elements:

In[1]:=
ResourceFunction["SelectPositions"][{1, 2, 4, 7, 6, 2}, EvenQ]
Out[1]=

Use a pure function to test each element:

In[2]:=
ResourceFunction["SelectPositions"][{1, 2, 4, 7, 6, 2}, # > 2 &]
Out[2]=

Return only the first position selected:

In[3]:=
ResourceFunction["SelectPositions"][{1, 2, 4, 7, 6, 2}, # > 2 &, 1]
Out[3]=

Use the operator form of SelectPositions:

In[4]:=
ResourceFunction["SelectPositions"][EvenQ][{1, 2, 4, 7, 6, 2}]
Out[4]=

SelectPositions operates on values in an Association:

In[5]:=
ResourceFunction[
 "SelectPositions"][<|a -> 1, b -> 2, c -> 3, d -> 4|>, # > 2 &]
Out[5]=

Scope (4) 

SelectPositions picks out positions for which applying the criterion explicitly yields True:

In[6]:=
ResourceFunction["SelectPositions"][{1, 2, 4, 7, x}, # > 2 &]
Out[6]=

Applying the criterion to the symbolic object x does not explicitly yield True:

In[7]:=
x > 2
Out[7]=

Find positions of pairs containing x:

In[8]:=
ResourceFunction[
 "SelectPositions"][{{1, y}, {2, x}, {3, x}, {4, z}, {5, x}}, MemberQ[#, x] &]
Out[8]=

Find up to two pairs containing x:

In[9]:=
ResourceFunction[
 "SelectPositions"][{{1, y}, {2, x}, {3, x}, {4, z}, {5, x}}, MemberQ[#, x] &, 2]
Out[9]=

Fewer than the requested elements may be returned:

In[10]:=
ResourceFunction[
 "SelectPositions"][{{1, y}, {2, x}, {3, x}, {4, z}, {5, x}}, MemberQ[#, z] &, 2]
Out[10]=

Use an operator form as selection criterion:

In[11]:=
ResourceFunction["SelectPositions"][Range[10], GreaterThan[3]]
Out[11]=

Use SelectPositions in operator form:

In[12]:=
Range[10] // ResourceFunction["SelectPositions"][GreaterThan[3]]
Out[12]=

Generalizations and Extensions (2) 

SelectPositions works with any head, not just List:

In[13]:=
ResourceFunction["SelectPositions"][f[1, a, 2, b, 3], IntegerQ]
Out[13]=

SelectPositions works with SparseArray objects:

In[14]:=
s = SparseArray[Table[2^i -> i, {i, 0, 5}]]
Out[14]=
In[15]:=
ResourceFunction["SelectPositions"][s, EvenQ]
Out[15]=
In[16]:=
ResourceFunction["SelectPositions"][s, OddQ]
Out[16]=

Applications (5) 

Get positions of elements that match a criterion for use in other functions:

In[17]:=
expr = f[1, a, 2, b, 3]
Out[17]=
In[18]:=
pos = ResourceFunction["SelectPositions"][expr, IntegerQ]
Out[18]=

Use with Extract:

In[19]:=
Extract[expr, pos]
Out[19]=

Use with ReplacePart:

In[20]:=
ReplacePart[expr, pos -> n]
Out[20]=

Use with MapAt:

In[21]:=
MapAt[g, expr, pos]
Out[21]=

Use with Insert:

In[22]:=
Insert[expr, n, pos]
Out[22]=

Properties and Relations (3) 

SelectPositions is similar to Position except that it uses a function instead of a pattern:

In[23]:=
list = RandomInteger[9, {10, 2}]
Out[23]=

Select the position of lists that have a sum of elements less than 10:

In[24]:=
f = Total[#] < 10 &;
ResourceFunction["SelectPositions"][list, f]
Out[25]=

Use Position to get the same result:

In[26]:=
Position[list, x_ /; f[x], {1}, Heads -> False]
Out[26]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 03 December 2018

Related Resources

License Information