Wolfram: Position in a List
Index of a Element
Index is an integer, that indicates the nth element.
For example, if we have
{a, {{b,c}, d}, e}
- index
1
isa
- index
2
is{{b,c}, d}
- index
3
ise
Position of a Subexpression
A position is a list of integers that uniquely specify a part of expression.
For example, if we have
{a, {{b,c}, d}, e}
- position
{1}
isa
- position
{2}
is{{b,c}, d}
- position
{2,1}
is{b,c}
- position
{2,2}
isd
- position
{2,1,1}
isb
- position
{2,1,2}
isc
- position
{3}
ise
Index counting start at 1. Index 0 means the Head of Expression.
Get Position of a Element
Position[expr, pattern]
-
Return a list of positions that Pattern occur in expression. If no exist, return a empty list.
Position[expr, pattern, levelSpec]
→ limit to Level Spec.Position[expr, pattern, levelSpec, n]
→ return first n.
(* position of e is {3} *) Position[ {a,{{b,c},a},e}, e ] (* {{3}} *) (* position of a is {1} and {2, 2} *) Position[ {a,{{b,c},a},e}, a ] (* {{1}, {2, 2}} *) (* when not found, empty list *) Position[ {a,{{b,c},a},e}, m ] (* {} *) (* position of b *) Position[ {a,{{b,c},a},e}, b ] (* {{2, 1, 1}} *) (* position of {b, c} *) Position[ {a,{{b,c},a},e}, {b, c} ] (* {{2, 1}} *)
Limit Depth Level
(* find 7, only in level 1 *) Position[ {7, {{2, 7}, 2}, 7}, 7, {1} ] (* {{1}, {3}} *) (* find 7, only in level 2 *) Position[ {7, {{2, 7}, 2}, 7}, 7, {2} ] (* {} *) (* find 7, only in level 3 *) Position[ {7, {{2, 7}, 2}, 7}, 7, {3} ] (* {{2, 1, 2}} *) (* find 7, in all levels *) Position[ {7, {{2, 7}, 2}, 7}, 7, {1, Infinity} ] (* {{1}, {2, 1, 2}, {3}} *)