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

# Adding, Removing and Modifying List Elements

 Prepend[list,element] add element at the beginning of list Append[list,element] add element at the end of list Insert[list,element,i] insert element at position i in list Insert[list,element,-i] insert at position i counting from the end of list Riffle[list,element] interleave element between the entries of list Delete[list,i] delete the element at position i in list ReplacePart[list,i->new] replace the element at position i in list with new ReplacePart[list,{i,j}->new] replace list[[i, j]] with new

Functions for manipulating elements in explicit lists.

This gives a list with x prepended.
 Out[1]=
This inserts x so that it becomes element number 2.
 Out[2]=
This interleaves x between the entries of the list.
 Out[3]=
This replaces the third element in the list with x.
 Out[4]=
This replaces the 1, 2 element in a 2×2 matrix.
 Out[5]=
Functions like ReplacePart 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 the ith element

Resetting list elements.

This defines v to be a list.
 Out[6]=
This sets the third element to be x.
 Out[7]=
Now v has been changed.
 Out[8]=
 m[[i,j]]=new replace the (i, j)th element of a matrix m[[i]]=new replace the ith row m[[All,i]]=new replace the ith column

Resetting pieces of matrices.

This defines m to be a matrix.
 Out[9]=
This sets the first column of the matrix.
 Out[10]=
This sets every element in the first column to be 0.
 Out[11]=