21.12 Finding Positions in Lists

  • Position( list, obj[, from] ) O

    returns the position of the first occurrence obj in list, or fail if obj is not contained in list. If a starting index from is given, it returns the position of the first occurrence starting the search after position from.

    Each call to the two argument version is translated into a call of the three argument version, with third argument the integer zero 0. (Methods for the two argument version must be installed as methods for the version with three arguments, the third being described by IsZeroCyc.)

    gap> Position( [ 2, 2, 1, 3 ], 1 );
    3
    gap> Position( [ 2, 1, 1, 3 ], 1 );
    2
    gap> Position( [ 2, 1, 1, 3 ], 1, 2 );
    3
    gap> Position( [ 2, 1, 1, 3 ], 1, 3 );
    fail
    

  • PositionCanonical( list, obj ) O

    returns the position of the canonical associate of obj in list. The definition of this associate depends on list. For internally represented lists it is defined as the element itself (and PositionCanonical thus defaults to Position, see Position), but for example for certain enumerators (see Enumerators) other canonical associates can be defined.

    For example RightTransversal defines the canonical associate to be the element in the transversal defining the same coset of a subgroup in a group.

    gap> g:=Group((1,2,3,4),(1,2));;u:=Subgroup(g,[(1,2)(3,4),(1,3)(2,4)]);;
    gap> rt:=RightTransversal(g,u);;AsList(rt);
    [ (), (3,4), (2,3), (2,3,4), (2,4,3), (2,4) ]
    gap> Position(rt,(1,2));
    fail
    gap> PositionCanonical(rt,(1,2));
    2
    

  • PositionNthOccurrence( list, obj, n ) O

    returns the position of the n-th occurrence of obj in list and returns fail if obj does not occur n times.

    gap> PositionNthOccurrence([1,2,3,2,4,2,1],1,1);
    1
    gap> PositionNthOccurrence([1,2,3,2,4,2,1],1,2);
    7
    gap> PositionNthOccurrence([1,2,3,2,4,2,1],2,3);
    6
    gap> PositionNthOccurrence([1,2,3,2,4,2,1],2,4);
    fail
    

  • PositionSorted( list, elm ) O
  • PositionSorted( list, elm, func ) O

    In the first form PositionSorted returns the position of the element elm in the sorted list list.

    In the second form PositionSorted returns the position of the element elm in the list list, which must be sorted with respect to func. func must be a function of two arguments that returns true if the first argument is less than the second argument and false otherwise.

    PositionSorted returns pos such that list [pos -1] < elm and elm £ list [pos ]. That means, if elm appears once in list, its position is returned. If elm appears several times in list, the position of the first occurrence is returned. If elm is not an element of list, the index where elm must be inserted to keep the list sorted is returned.

    PositionSorted uses binary search, whereas Position can in general use only linear search, see the remark at the beginning of Sorted Lists and Sets. For sorting lists, see Sorting Lists, for testing whether a list is sorted, see IsSortedList and IsSSortedList.

    gap> PositionSorted( [1,4,5,5,6,7], 0 );
    1
    gap> PositionSorted( [1,4,5,5,6,7], 2 );
    2
    gap> PositionSorted( [1,4,5,5,6,7], 4 );
    2
    gap> PositionSorted( [1,4,5,5,6,7], 5 );
    3
    gap> PositionSorted( [1,4,5,5,6,7], 8 );
    7
    

  • PositionSet( list, obj ) F
  • PositionSet( list, obj, func ) F

    PositionSet is a slight variation of PositionSorted. The only difference to PositionSorted is that PositionSet returns fail if obj is not in list.

    gap> PositionSet( [1,4,5,5,6,7], 0 );
    fail
    gap> PositionSet( [1,4,5,5,6,7], 2 );
    fail
    gap> PositionSet( [1,4,5,5,6,7], 4 );
    2
    gap> PositionSet( [1,4,5,5,6,7], 5 );
    3
    gap> PositionSet( [1,4,5,5,6,7], 8 );
    fail
    

  • PositionProperty( list, func ) O

    returns the first position of an element in the list list for which the property tester function func returns true.

    gap> PositionProperty( [10^7..10^8], IsPrime );
    20
    gap> PositionProperty( [10^5..10^6],
    >        n -> not IsPrime(n) and IsPrimePowerInt(n) );
    490
    
    First (see First) allows you to extract the first element of a list that satisfies a certain property.

  • PositionBound( list ) O

    returns the first index for which an element is bound in the list list. For the empty list it returns fail.

    gap> PositionBound([1,2,3]);
    1
    gap> PositionBound([,1,2,3]);
    2
    

  • PositionNot( list, val[, from-minus-one] ) O

    For a list list and an object val, PositionNot returns the smallest nonnegative integer n such that list [n] is either unbound or not equal to val. If a nonnegative integer is given as optional argument from-minus-one then the first position larger than from-minus-one with this property is returned.

  • PositionNonZero( vec ) O

    For a row vector vec, PositionNonZero returns the position of the first non-zero element of vec, or Length(vec)+1 if all entries of vec are zero.

    PositionNonZero implements a special case of PositionNot (see PositionNot). Namely, the element to be avoided is the zero element, and the list must be (at least) homogeneous because otherwise the zero element cannot be specified implicitly.

    gap> l:= [ 1, 1, 2, 3, 2 ];;  PositionNot( l, 1 );
    3
    gap> PositionNot( l, 1, 4 );  PositionNot( l, 2, 5 );
    5
    6
    gap> PositionNonZero( l );  PositionNonZero( [ 2, 3, 4, 5 ] * Z(2) );
    1
    2
    

  • PositionSublist( list, sub ) O
  • PositionSublist( list, sub, from ) O

    returns the smallest index in the list list at which a sublist equal to sub starts. If the substring does not occur the operation returns fail. The second version starts searching after position from.

    [Top] [Previous] [Up] [Next] [Index]

    GAP 4 manual
    February 2000