 |  |
1.8.10 Grouping and Combining Elements of Lists
| Partition[list, n] | partition list into n-element pieces | | Partition[list, n, d] | use offset d for successive pieces | | Split[list] | split list into pieces consisting of runs of identical elements |
Functions for grouping together elements of lists. | Here is a list. | |
In[1]:=
t = {a, b, c, d, e, f, g}
|
Out[1]=
|
|
| This groups the elements of the list in pairs, throwing away the single element left at the end. | |
Out[2]=
|
|
| This groups elements in triples. There is no overlap between the triples. | |
Out[3]=
|
|
| This makes triples of elements, with each successive triple offset by just one element. | |
In[4]:=
Partition[t, 3, 1]
|
Out[4]=
|
|
| This splits up the list into runs of identical elements. | |
In[5]:=
Split[{a, a, b, b, b, a, a, a, b}]
|
Out[5]=
|
|
| Tuples[list, n] | generate all possible -tuples of elements from list | Tuples[{ , , ... }] | generate all tuples whose  element is from |
Finding possible tuples of elements in lists. | This gives all possible ways of picking two elements out of the list. | |
In[6]:=
Tuples[{a, b}, 2]
|
Out[6]=
|
|
| This gives all possible ways of picking one element from each list. | |
In[7]:=
Tuples[{{a, b}, {1, 2, 3}}]
|
Out[7]=
|
|
|
|
|