Partition
Usage
• Partition[list, n] 划分列表为长度为 n 的非重叠子列表。
• Partition[list, n, d] 产生偏移量为 d 的子列表。
• Partition[list,  , , ... ] 划分一个嵌套列表为大小为  的块。
• Partition[list,  , , ... ,  , , ... ] 在 list 中层 i上使用偏移量  。
• Partition[list, n, d,  ,  ] 指定 list的第一个元素应当出现在第一个子列表的位置  , list的最后一个元素应当出现在最后一个子列表的位置  或其后。如果需要附加元素, Partition通过对待 list为循环来填充它们。
• 如果需要, Partition[list, n, d,  ,  , x] 重复元素 x来填补。
Notes
• 由Partition[list, n, d]产生的所有子列表都有长度 n。在list末尾的一些元素因而可能不出现在任何子列表中。 • Partition[ a,b,c,d,e , 2]  中的元素 e被扔掉。 • list中的所有元素出现在由Partition[list, n, 1]产生的子列表中。 • 如果d大于Partition[list, n, d]中的 n,则在list中间的元素被跳过。 • Partition[list, 1, d]以和 Take[list, 1, -1, d ]相同的方式挑选元素。 • Partition[list, n, d,  ,  ] effectively allows sublists that have overhangs that extend past the beginning or end of list. • Partition[list, n, d,  ,  ] 有效地允许具有超出 list的开头或结尾的扩展的子列表。 • Partition[list, n, d, k] 等于 Partition[list, n, d, k, k ].
| "\!\(\*StyleBox[\"\\\"{1,\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"-1}\\\"\", \"MR\"]\) " | 不允许超出 | | "\!\(\*StyleBox[\"\\\"{1,\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"1}\\\"\", \"MR\"]\) " | ""在结尾允许最大超出 | | "\!\(\*StyleBox[\"\\\"{-1,\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"-1}\\\"\", \"MR\"]\) " | 在开头允许最大超出 | | "\!\(\*StyleBox[\"\\\"{-1,\\\"\", \"MR\"]\) \!\(\*StyleBox[\"\\\"1}\\\"\", \"MR\"]\) " | 在开头和结尾允许最大超出 |
• Partition[list, n, d,  ,  , padlist] 有效地放置 padlist的重复拷贝,然后在它们上强加一个 list的拷贝,并划分结果。 • padlist的公共选项是:
| "\!\(\*StyleBox[\"\\\"x\\\"\", \"TI\"]\) " | 用重复单一元素填补 | | {"\!\(\*StyleBox[\"\\\"x\\\"\", \"TI\"]\)"1"\!\(\*StyleBox[\"\\\",\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"x\\\"\", \"TI\"]\)"2"\!\(\*StyleBox[\"\\\",\\\"\", \"MR\"]\) ... "}" " | 用循环重复元素序列填补 | | "\!\(\*StyleBox[\"\\\"list\\\"\", \"TI\"]\) " | 处理为循环填补缺省 | | "\!\(\*StyleBox[\"\\\"{}\\\"\", \"MR\"]\) " | 不填补,潜在的留下不同长度子列表 |
• Partition[ a,b,c,d ,2,1, -1,1 , ]  .
• 如果 list 有长度 s, 则 Partition[list, n, d] 产生子列表 Max[0, Floor[(s + d - n)/d]] . • Partition[list,  , , ... ,  ] 有效地在 list中的层 r 上用深度为 r 的临近元素的嵌套列表替换元素块。 • 如果没有指定元素块,临域是相邻和不重叠的。 • Partition[list,  , , ... , d] 在每层使用偏移量 d . • Partition[list, nlist, dlist,   , , ... ,   , , ...  ] 指定 list中的元素 1,1,...  应当出现在结果的块 1,1,...  的位置  , , ...  ,而 list的元素 -1,-1,...  应当出现在结果的块 -1,-1,...  的位置  , , ...  或其后。 • Partition[list,  , , ... ,  , klist, padlist] 有效地构造一个深度为 r的 padlist的拷贝数组,然后在它们上强加一个 list的拷贝,并划分结果。 • 如果 list 有维  , , ... ,   则 Partition[list,  , , ... ,  ]有维 , , ... , , , , ... ,   其中  由 Floor[ / ]给出.
• 对象 list 不必有头 List. • Partition[f[a,b,c,d], 2]  . • 参见 Mathematica 全书 : 节 1.8.10.
Further Examples
This groups the elements of the original list in pairs; since there is an odd number of elements, the last element is thrown away.
In[1]:=
|
Out[1]=
|
This makes triples of elements, with each successive triple offset by elements.
In[2]:=
|
Out[2]=
|
Here the offset is .
In[3]:=
|
Out[3]=
|
This defines a x matrix.
In[4]:=
|
Out[4]//MatrixForm=
|
This partitions the matrix into blocks of size x . The extra elements that do not fit are thrown away; here these are the last row and last column.
In[5]:=
|
Out[5]//MatrixForm=
|
The indicates an offset at level (the rows) and at level (the columns).
In[6]:=
|
Out[6]//MatrixForm=
|
Here the element a appears at position of the block and the element y appears at position of the block.
In[7]:=
|
Out[7]//MatrixForm=
|
This pads with A, B, C.
In[8]:=
|
Out[8]//MatrixForm=
|
Using Partition to make a cylinder Here is the direct way to make a cylinder. This loads the package Shapes.
In[9]:=
|
Evaluate the cell to see the graphic.
In[10]:=
|
Here is another approach using Partition. The function mycylinder[r, h, n] gives the graphic for a cylinder of radius r, height h approximated by n vertical rectangles.
In[11]:=
|
Evaluate the cell to see the graphic.
In[12]:=
|
In[13]:=
|
|