Movatterモバイル変換


[0]ホーム

URL:


WOLFRAM

Wolfram Language & System Documentation Center
Lists

Lists

Making Lists of Objects
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.
Here is a list of three numbers:
This squares each number in the list, and adds1 to it:
This takes differences between corresponding elements in the two lists. The lists must be the same length:
The value of% is the whole list:
You can apply any of the mathematical functions in"Some Mathematical Functions" to whole lists:
Just as you can set variables to be numbers, so also you can set them to be lists.
This assignsv to be a list:
Whereverv appears, it is replaced by the list:
Collecting Objects Together
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.
Here is a list of numbers:
This gives a list of symbolic expressions:
You can differentiate these expressions:
And then you can find values whenx is replaced with3:
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.
Making Tables of Values
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.
This gives a table of the values of, with running from 1 to 6:
Here is a table of for from to:
This gives the numerical values:
You can also make tables of formulas:
Table uses exactly the same iterator notation as the functionsSum andProduct, which are discussed in"Sums and Products":
This makes a table with values ofx running from0 to1 in steps of0.25:
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".
This makes a table of with running from 1 to 3 and running from 1 to 2:
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.
Sometimes you may want to generate a table by evaluating a particular expression many times, without incrementing any variables.
This creates a list containing four copies of the symbolx:
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:
This evaluates for each of the values ofi in the list{1,4,9,16}:
This creates a 3×2 table:
In this table, the length of the rows depends on the more slowly varying iterator variable,i:
You can useTable to generate arrays with any number of dimensions.
This generates a threedimensional 2×2×2 array. It is a list of lists of lists:
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
Functions for generating tables.
You can use the operations discussed in"Manipulating Elements of Lists" to extract elements of the table.
This creates a table and gives it the namesq:
This gives the third part of the table:
This gives a list of the third through fifth parts:
This creates a 2×2 table, and gives it the namem:
This extracts the first sublist from the list of lists that makes up the table:
This extracts the second element of that sublist:
This does the two operations together:
This displaysm in a "tabular" form:
t[[i]]
or
Part[t,i]
give theith sublist int(also input asti)
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]]
Ways to extract parts of tables.
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 twodimensional arrays. When you lay them out in a tabular form, the two indices of each element are like its and coordinates.
Manipulating Elements of Lists
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
Operations on list elements.
This extracts the second element of the list:
This extracts a list of elements:
This assigns the value ofv to be a list:
You can extract elements ofv:
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
Arraylike operations on lists.
Here is a list:
This resets the third element of the list:
Now the list assigned tov has been modified:
Vectors and Matrices
Vectors and matrices in the Wolfram Language are simply represented by lists and by lists of lists, respectively.
{a,b,c}
vector
{{a,b},{c,d}}
matrix
The representation of vectors and matrices by lists.
This is a 2×2 matrix:
Here is the first row:
Here is the element:
This is a twocomponent vector:
The objectsp andq are treated as scalars:
Vectors are added component by component:
This gives the dot (scalar) product of two vectors:
You can also multiply a matrix by a vector:
Or a matrix by a matrix:
Or a vector by a matrix:
This combination makes a scalar:
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 lengthn vector by evaluatingf withi=1,2,,n
Array[a,n]
build a lengthn 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
Functions for vectors.
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
Functions for matrices.
Column[list]
display the elements oflist in a column
MatrixForm[list]
displaylist in matrix form
Formatting constructs for vectors and matrices.
This builds a 3×3 matrix with elements:
This displayss in standard twodimensional matrix format:
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:
Here are the dimensions of the matrix on the previous line:
This generates a 3×3 diagonal matrix:
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
Some mathematical operations on matrices.
Here is the 2×2 matrix of symbolic variables that was defined:
This gives its determinant:
Here is the transpose ofm:
This gives the inverse ofm in symbolic form:
Here is a 3×3 rational matrix:
This gives its inverse:
Taking the dot product of the inverse with the original matrix gives the identity matrix:
Here is a 3×3 matrix:
Eigenvalues gives the eigenvalues of the matrix:
This gives a numerical approximation to the matrix:
Here are numerical approximations to the eigenvalues:
"Linear Algebra in Wolfram Language" discusses many other matrix operations that are built into the Wolfram Language.
Getting Pieces of Lists
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,
Picking out elements of lists.
We will use this list for the examples:
Here is the last element oft:
This gives the third element:
This gives the list of elements3 through6:
This gives a list of the first and fourth elements:
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
Picking out sequences in lists.
This gives the first three elements of the listt defined above:
This gives the last three elements:
This gives elements2 through5 inclusive:
This gives elements3 through7 in steps of2:
This givest with the first element dropped:
This givest with its first three elements dropped:
This givest with only its third element 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.
Testing and Searching List Elements
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
Testing and searching for elements of lists.
"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.
This gives a list of the positions at whicha appears in the list:
Count counts the number of occurrences ofa:
This shows thata is an element of{a,b,c}:
On the other hand,d is not:
This assignsm to be the 3×3 identity matrix:
This shows that0 does occursomewhere inm:
This gives a list of the positions at which0 occurs inm:
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".
Adding, Removing, and Modifying List Elements
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 for manipulating elements in explicit lists.
This gives a list withx prepended:
This insertsx so that it becomes element number 2:
This interleavesx between the entries of the list:
This replaces the third element in the list withx:
This replaces the 1, 2 element in a 2×2 matrix:
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.
v={e1,e2,}
assign a variable to be a list
v[[i]]=new
assign a new value to theith element
Resetting list elements.
This definesv to be a list:
This sets the third element to bex:
Nowv has been changed:
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
Resetting pieces of matrices.
This definesm to be a matrix:
This sets the first column of the matrix:
This sets every element in the first column to be0:
Combining Lists
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
Functions for combining lists.
Join concatenates any number of lists together:
Union combines lists, keeping only distinct elements:
Riffle combines lists by interleaving their elements:
Lists as Sets
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
Set theoretical functions.
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:
This gives all the subsets of the list:
DeleteDuplicates deletes all duplicate elements from the list:
Rearranging Lists
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
Functions for rearranging lists.
This sorts the elements of a list into a standard order. In simple cases like this, the order is alphabetical or numerical:
This sorts the elements, removing any duplicates:
This rotates ("shifts") the elements in the list two places to the left:
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
Padding lists.
This pads a list withx's to make it length 10:
Grouping and Combining Elements of Lists
Partition[list,n]
partitionlist intonelement 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
Functions for grouping together elements of lists.
Here is a list:
This groups the elements of the list in pairs, throwing away the single element left at the end:
This groups elements in triples. There is no overlap between the triples:
This makes triples of elements, with each successive triple offset by just one element:
This splits up the list into runs of identical elements:
Here is a random list of integers:
This splits the list into runs of even and odd numbers:
Gather the integers into sublists:
Gather the integers into sublists based on equivalence mod 3:
Tuples[list,n]
generate all possiblentuples of elements fromlist
Tuples[{list1,list2,}]
generate all tuples whoseith element is fromlisti
Finding possible tuples of elements in lists.
This gives all possible ways of picking two elements out of the list:
This gives all possible ways of picking one element from each list:
Ordering in Lists
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
Ordering in lists.
Here is a list of numbers:
This gives the elements oft in sorted order:
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]:
This gives the smallest element in the list:
Rearranging Nested Lists
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
A few functions for rearranging nested lists.
This "flattens out" sublists. You can think of it as effectively just removing all inner braces:
This flattens out only one level of sublists:
There are many other operations you can perform on nested lists. More operations are discussed in"Manipulating Lists".

Related Guides

Top

[8]ページ先頭

©2009-2025 Movatter.jp