Mathematica 教程 函数 »|教程 »| 更多关于 »

Testing and Searching List Elements

 Position[list,form] the positions at which form occurs in list Count[list,form] the number of times form appears as an element of list MemberQ[list,form] test whether form is an element of list FreeQ[list,form] test whether form occurs nowhere in list

Testing and searching for elements of lists.

"Getting Pieces of Lists" discusses how to extract pieces of lists based on their positions or indices. Mathematica also has functions that search and test for elements of lists, based on the values of those elements.
This gives a list of the positions at which a appears in the list.
 Out[1]=
Count counts the number of occurrences of a.
 Out[2]=
This shows that a is an element of {a, b, c}.
 Out[3]=
On the other hand, d is not.
 Out[4]=
This assigns m to be the 3×3 identity matrix.
 Out[5]=
This shows that 0 does occur somewhere in m.
 Out[6]=
This gives a list of the positions at which 0 occurs in m.
 Out[7]=
As discussed in "Finding Expressions That Match a Pattern", the functions Count and Position, as well as MemberQ and FreeQ, can be used not only to search for particular list elements, but also to search for classes of elements which match specific "patterns".