Skip to main content

list manipulation - Confused about how Partition works


I have a problem about the padding list about Partition command. For instance,


Partition[{a, b, c, d, e, f, g}, 3, 1, {-2, 1}, {x, y, z}]


{{z, a, b}, {a, b, c}, {b, c, d}, {c, d, e}, {d, e, f}, {e, f, g}, {f,g, y}, {g, y, z}}

However, my understanding about padding list shown as below:




enter image description here



Another example


Partition[{a, b, c, d, e, f}, 3, 3, {1, 1}]


 {{a, b, c}, {d, e, f}}

My understanding:




enter image description here



Question


Can someone give me a good explanation?



Answer



Example #1


Let me make your example a bit smaller for brevity:


Partition[{a, b, c, d}, 3, 1, {-2, 1}, {x, y, z}]



{{z, a, b}, {a, b, c}, {b, c, d}, {c, d, y}, {d, y, z}}

This is in effect:


PadRight[{a, b, c, d}, 7, {x, y, z}, 1]

Partition[%, 3, 1]


{z, a, b, c, d, y, z}


{{z, a, b}, {a, b, c}, {b, c, d}, {c, d, y}, {d, y, z}}

Think instead:


enter image description here +
enter image description here Equals
enter image description here


Example #2


The documentation states:


enter image description here



Critical to your example is: "appear at or after position kR in the last sublist."


Therefore in this case it does not matter if kR is any of: 1, 2, 3, -1, -2, -3, because each will be satisfied by:


{{a, b, c}, {d, e, f}}

Partition does not violate the cyclic order of the list. For the output to be as you anticipated:


{{a, b, c}, {d, e, f}, {f, a, b}}

The element f would have to be taken out of sequence; the next element should be a.


Comments