Lists
Lists
In doing calculations, it is often convenient to collect together several objects, and treat them as a single entity.Lists give you a way to make collections of objects in the Wolfram Language. As you will see later, lists are very important and general structures in the Wolfram Language.
A list such as{3,5,1} is a collection of three objects. But in many ways, you can treat the whole list as a single object. You can, for example, do arithmetic on the whole list at once, or assign the whole list to be the value of a variable.
This takes differences between corresponding elements in the two lists. The lists must be the same length:
You can apply any of the mathematical functions in"Some Mathematical Functions" to whole lists:
We first encountered lists in"Making Lists of Objects" as a way of collecting numbers together. Here, we shall see many different ways to use lists. You will find that lists are some of the most flexible and powerful objects in the Wolfram Language. You will see that lists in the Wolfram Language represent generalizations of several standard concepts in mathematics and computer science.
At a basic level, what a Wolfram Language list essentially does is to provide a way for you to collect together several expressions of any kind.
The mathematical functions that are built into the Wolfram Language are mostly set up to be "listable" so that they act separately on each element of a list. This is, however, not true of all functions in the Wolfram Language. Unless you set it up specially, a new functionf that you introduce will treat lists just as single objects."Applying Functions to Parts of Expressions" and"Structural Operations" will describe how you can useMap andThread to apply a function like this separately to each element in a list.
You can use lists as tables of values. You can generate the tables, for example, by evaluating an expression for a sequence of different parameter values.
Table uses exactly the same iterator notation as the functionsSum andProduct, which are discussed in"Sums and Products":
You can perform other operations on the lists you get fromTable:
TableForm displays lists in a "tabular" format. Notice that both words in the nameTableForm begin with capital letters:
All the examples so far have been of tables obtained by varying a single parameter. You can also make tables that involve several parameters. These multidimensional tables are specified using the standard Wolfram Language iterator notation, discussed in"Sums and Products".
The table in this example is alist of lists. The elements of the outer list correspond to successive values of
. The elements of each inner list correspond to successive values of
, with
fixed.
. The elements of each inner list correspond to successive values of
, with
fixed.Sometimes you may want to generate a table by evaluating a particular expression many times, without incrementing any variables.
This gives a list of four pairs of numbers sampled from{1,2,3,4}.Table reevaluatesRandomSample[{1,2,3,4},2] for each element in the list, so that you get four different samples:
You can useTable to generate arrays with any number of dimensions.
| Table[f,{imax}] | give a list ofimax values off |
| Table[f,{i,imax}] | give a list of the values off asi runs from1 toimax |
| Table[f,{i,imin,imax}] | give a list of values withi running fromimin toimax |
| Table[f,{i,imin,imax,di}] | use steps ofdi |
| Table[f,{i,imin,imax},{j,jmin,jmax},…] | generate a multidimensional table |
| Table[f,{i,{i1,i2,…}] | give a list of the values off asi successively takes the valuesi1,i2,… |
| TableForm[list] | display a list in tabular form |
You can use the operations discussed in"Manipulating Elements of Lists" to extract elements of the table.
| t[[i]] or Part[t,i] | give theith sublist int(also input ast〚i〛) |
| t[[i;;j]] or Part[t,i;;j] | give a list of the partsi throughj |
| t[[{i1,i2,…}]] or Part[t,{i1,i2,…}] | give a list of thei1th,i2th,… parts oft |
| t[[i,j,…]] or Part[t,i,j,…] | give the part oft corresponding tot[[i]][[j]]… |
As mentioned in"Manipulating Elements of Lists", you can think of lists in the Wolfram Language as being analogous to "arrays". Lists of lists are then like two‐dimensional arrays. When you lay them out in a tabular form, the two indices of each element are like its
and
coordinates.
and
coordinates.Many of the most powerful list manipulation operations in the Wolfram Language treat whole lists as single objects. Sometimes, however, you need to pick out or set individual elements in a list.
You can refer to an element of a Wolfram Language list by giving its "index". The elements are numbered in order, starting at 1.
| {a,b,c} | a list |
| Part[list,i] or list[[i]] | theith element oflist(the first element islist[[1]]) |
| Part[list,{i,j,…}] or list[[{i,j,…}]] | a list of theith,jth,… elements oflist |
| Part[list,i;;j] | a list of theith throughjth elements oflist |
By assigning a variable to be a list, you can use Wolfram Language lists much like "arrays" in other computer languages. Thus, for example, you can reset an element of a list by assigning a value tov[[i]].
| Part[v,i] or v[[i]] | extract theith element of a list |
| Part[v,i]=value or v[[i]]=value | reset theith element of a list |
Vectors and matrices in the Wolfram Language are simply represented by lists and by lists of lists, respectively.
Because of the way the Wolfram Language uses lists to represent vectors and matrices, you never have to distinguish between "row" and "column" vectors.
| Table[f,{i,n}] | build a length‐n vector by evaluatingf withi=1,2,…,n |
| Array[a,n] | build a length‐n vector of the form{a[1],a[2],…} |
| Range[n] | create the list{1,2,3,…,n} |
| Range[n1,n2] | create the list{n1,n1+1,…,n2} |
| Range[n1,n2,dn] | create the list{n1,n1+dn,…,n2} |
| list[[i]] or Part[list,i] | give theith element in the vectorlist |
| Length[list] | give the number of elements inlist |
| cv | multiply a vector by a scalar |
| a.b | dot product of two vectors |
| Cross[a,b] | cross product of two vectors(also input asa×b) |
| Norm[v] | Euclidean norm of a vector |
| Table[f,{i,m},{j,n}] | build anm×n matrix by evaluatingf withi ranging from1 tom andj ranging from1 ton |
| Array[a,{m,n}] | build anm×n matrix withi,jth elementa[i,j] |
| IdentityMatrix[n] | generate ann×n identity matrix |
| DiagonalMatrix[list] | generate a square matrix with the elements inlist on the main diagonal |
| list[[i]] or Part[list,i] | give theith row in the matrixlist |
| list[[All,j]] or Part[list,All,j] | give thejth column in the matrixlist |
| list[[i,j]] or Part[list,i,j] | give thei,jth element in the matrixlist |
| Dimensions[list] | give the dimensions of a matrix represented bylist |
| Column[list] | display the elements oflist in a column |
| MatrixForm[list] | displaylist in matrix form |
This gives a vector with symbolic elements. You can use this in deriving general formulas that are valid with any choice of vector components:
This gives a 3×2 matrix with symbolic elements."Building Lists from Functions" discusses how you can produce other kinds of elements withArray:
| cm | multiply a matrix by a scalar |
| a.b | dot product of two matrices |
| Inverse[m] | matrix inverse |
| MatrixPower[m,n] | nth power of a matrix |
| Det[m] | determinant |
| Tr[m] | trace |
| Transpose[m] | transpose |
| Eigenvalues[m] | eigenvalues |
| Eigenvectors[m] | eigenvectors |
Eigenvalues gives the eigenvalues of the matrix:
"Linear Algebra in Wolfram Language" discusses many other matrix operations that are built into the Wolfram Language.
| First[list] | the first element inlist |
| Last[list] | the last element |
| Part[list,n] or list[[n]] | thenth element |
| Part[list,-n] or list[[-n]] | thenth element from the end |
| Part[list,m;;n] | elementsm throughn |
| Part[list,{n1,n2,…}] or list[[{n1,n2,…}]] | |
the list of elements at positionsn1,n2,… | |
| Take[list,n] | the firstn elements inlist |
| Take[list,-n] | the lastn elements |
| Take[list,{m,n}] | elementsm throughn(inclusive) |
| Rest[list] | list with its first element dropped |
| Drop[list,n] | list with its firstn elements dropped |
| Most[list] | list with its last element dropped |
| Drop[list,-n] | list with its lastn elements dropped |
| Drop[list,{m,n}] | list with elementsm throughn dropped |
"Manipulating Expressions like Lists" shows how all the functions here can be generalized to work not only on lists, but on any Wolfram Language expressions.
The functions here allow you to pick out pieces that occur at particular positions in lists."Finding Expressions That Match a Pattern" shows how you can use functions likeSelect andCases to pick out elements of lists based not on their positions, but instead on their properties.
| Position[list,form] | the positions at whichform occurs inlist |
| Count[list,form] | the number of timesform appears as an element oflist |
| MemberQ[list,form] | test whetherform is an element oflist |
| FreeQ[list,form] | test whetherform occurs nowhere inlist |
"Getting Pieces of Lists" discusses how to extract pieces of lists based on their positions or indices. The Wolfram System also has functions that search and test for elements of lists, based on the values of those elements.
As discussed in"Finding Expressions That Match a Pattern", the functionsCount andPosition, as well asMemberQ andFreeQ, can be used not only to search forparticular list elements, but also to search for classes of elements which match specific "patterns".
| Prepend[list,element] | addelement at the beginning oflist |
| Append[list,element] | addelement at the end oflist |
| Insert[list,element,i] | insertelement at positioni inlist |
| Insert[list,element,-i] | insert at positioni counting from the end oflist |
| Riffle[list,element] | interleaveelement between the entries oflist |
| Delete[list,i] | delete the element at positioni inlist |
| ReplacePart[list,i->new] | replace the element at positioni inlist withnew |
| ReplacePart[list,{i,j}->new] | replacelist[[i,j]] withnew |
Functions likeReplacePart take explicit lists and give you new lists. Sometimes, however, you may want to modify a list "in place", without explicitly generating a new list.
| m[[i,j]]=new | replace the(i,j)th element of a matrix |
| m[[i]]=new | replace theith row |
| m[[All,i]]=new | replace theith column |
| Join[list1,list2,…] | concatenate lists together |
| Union[list1,list2,…] | combine lists, removing repeated elements and sorting the result |
| Riffle[list1,list2] | interleave elements oflist1 andlist2 |
Join concatenates any number of lists together:
Union combines lists, keeping only distinct elements:
Riffle combines lists by interleaving their elements:
The Wolfram Language usually keeps the elements of a list in exactly the order you originally entered them. If you want to treat a Wolfram Language list like a mathematicalset, however, you may want to ignore the order of elements in the list.
| Union[list1,list2,…] | give a list of the distinct elements in thelisti |
| Intersection[list1,list2,…] | give a list of the elements that are common to all thelisti |
| Complement[universal,list1,…] | give a list of the elements that are inuniversal, but not in any of thelisti |
| Subsets[list] | give a list of all subsets of the elements inlist |
| DeleteDuplicates[list] | delete all duplicates fromlist |
Union gives the elements that occur inany of the lists:
Intersection gives only elements that occur inall the lists:
Complement gives elements that occur in the first list, but not in any of the others:
DeleteDuplicates deletes all duplicate elements from the list:
| Sort[list] | sort the elements oflist into a standard order |
| Union[list] | sort elements, removing any duplicates |
| Reverse[list] | reverse the order of elements inlist |
| RotateLeft[list,n] | rotate the elements oflistn places to the left |
| RotateRight[list,n] | rotaten places to the right |
This sorts the elements of a list into a standard order. In simple cases like this, the order is alphabetical or numerical:
You can rotate to the right by giving a negative displacement, or by usingRotateRight:
| PadLeft[list,len,x] | padlist on the left withx to make it lengthlen |
| PadRight[list,len,x] | padlist on the right |
| Partition[list,n] | partitionlist inton‐element pieces |
| Partition[list,n,d] | use offsetd for successive pieces |
| Split[list] | splitlist into pieces consisting of runs of identical elements |
| SplitBy[list,f] | splitlist into runs of elements with identical values whenf is applied |
| Gather[list] | gather the elements oflist into sublists of identical elements |
| GatherBy[list,f] | gather the elements oflist into sublists with identical values whenf is applied |
| Tuples[list,n] | generate all possiblen‐tuples of elements fromlist |
| Tuples[{list1,list2,…}] | generate all tuples whoseith element is fromlisti |
| Sort[list] | sort the elements oflist into order |
| Ordering[list] | the positions inlist of the elements inSort[list] |
| Ordering[list,n] | the firstn elements ofOrdering[list] |
| Ordering[list,-n] | the lastn elements ofOrdering[list] |
| Permutations[list] | all possible orderings oflist |
| Min[list] | the smallest element inlist |
| Max[list] | the largest element inlist |
This gives the positions of the elements oft, from the position of the smallest to that of the largest:
This is the same asSort[t]:
You will encounter nested lists if you use matrices or generate multidimensional arrays and tables. The Wolfram Language provides many functions for handling such lists.
| Flatten[list] | flatten out all levels inlist |
| Flatten[list,n] | flatten out the topn levels inlist |
| Partition[list,{n1,n2,…}] | partition into blocks of sizen1×n2×… |
| Transpose[list] | interchange the top two levels of lists |
| RotateLeft[list,{n1,n2,…}] | rotate successive levels byni places |
| PadLeft[list,{n1,n2,…}] | pad successive levels to be lengthni |
There are many other operations you can perform on nested lists. More operations are discussed in"Manipulating Lists".
[8]ページ先頭
©2009-2025 Movatter.jp














