2.4.3 Nested Lists
{ , , ... } | list of lists | | Table[expr, {i, m}, {j, n}, ... ] | table of values of expr | | Array[f, {m, n, ... }] | array of values f[i, j, ... ] |
Normal[SparseArray[{{ , ,... } -> , ... }, {m, n, ... }]]
| | array with element { , ,... } being | Outer[f, , , ... ] | generalized outer product with elements combined using f | | Tuples[list, {m, n, ... }] | all possible arrays of elements from list |
Ways to construct nested lists. This generates a table corresponding to a nested list. | |
In[1]:=
Table[x^i + j, {i, 2}, {j, 3}]
|
Out[1]=
|
|
| This generates an array corresponding to the same nested list. | |
In[2]:=
Array[x^#1 + #2 &, {2, 3}]
|
Out[2]=
|
|
| Elements not explicitly specified in the sparse array are taken to be 0. | |
In[3]:=
Normal[SparseArray[{{1, 3} -> 3 + x}, {2, 3}]]
|
Out[3]=
|
|
| Each element in the final list contains one element from each input list. | |
In[4]:=
Outer[f, {a, b}, {c, d}]
|
Out[4]=
|
|
Functions like Array, SparseArray and Outer always generate full arrays, in which all sublists at a particular level are the same length.
| Dimensions[list] | the dimensions of a full array | | ArrayQ[list] | test whether all sublists at a given level are the same length | | ArrayDepth[list] | the depth to which all sublists are the same length |
Functions for full arrays. Mathematica can handle arbitrary nested lists. There is no need for the lists to form a full array. You can easily generate ragged arrays using Table. | This generates a triangular array. | |
In[5]:=
Table[x^i + j, {i, 3}, {j, i}]
|
Out[5]=
|
|
| Flatten[list] | flatten out all levels of list | | Flatten[list, n] | flatten out the top n levels |
Flattening out sublists. This generates a array. | |
Out[6]=
|
|
| Flatten in effect puts elements in lexicographic order of their indices. | |
Out[7]=
|
|
| Transpose[list] | transpose the top two levels of list | Transpose[list, { , , ... }] | put the k level in list at level |
Transposing levels in nested lists. This generates a array. | |
In[8]:=
Array[a, {2, 2, 2}]
|
Out[8]=
|
|
| This permutes levels so that level 3 appears at level 1. | |
In[9]:=
Transpose[%, {3, 1, 2}]
|
Out[9]=
|
|
| This restores the original array. | |
In[10]:=
Transpose[%, {2, 3, 1}]
|
Out[10]=
|
|
| Map[f, list, {n}] | map f across elements at level n | | Apply[f, list, {n}] | apply f to the elements at level n | | MapIndexed[f, list, {n}] | map f onto parts at level n and their indices |
Applying functions in nested lists. | Here is a nested list. | |
In[11]:=
m = {{{a, b}, {c, d}}, {{e, f}, {g, h}, {i}}};
|
|
| This maps a function f at level 2. | |
Out[12]=
|
|
| This applies the function at level 2. | |
In[13]:=
Apply[f, m, {2}]
|
Out[13]=
|
|
| This applies f to both parts and their indices. | |
In[14]:=
MapIndexed[f, m, {2}]
|
Out[14]=
|
|
Partition[list, { , , ... }] | partition into blocks | PadLeft[list, { , , ... }] | pad on the left to make an array | PadRight[list, { , , ... }] | pad on the right to make an array | RotateLeft[list, { , , ... }] | rotate places to the left at level k | RotateRight[list, { , , ... }] | rotate places to the right at level k |
Operations on nested lists. | Here is a nested list. | |
In[15]:=
m = {{{a, b, c}, {d, e}}, {{f, g}, {h}, {i}}};
|
|
| This rotates different amounts at each level. | |
In[16]:=
RotateLeft[m, {0, 1, -1}]
|
Out[16]=
|
|
This pads with zeros to make a array. | |
In[17]:=
PadRight[%, {2, 3, 3}]
|
Out[17]=
|
|
|