Closed JonasAlfredsson closed 4 years ago
While the code is very simple to use, the math (i.e. divmod
) is a bit more advanced, which is why I tried to be very detailed with what each variable does. If this is too verbose I am more than willing to try to shorten the bullet points even further, but then it might become a little bit too vague.
Hi,
I saw that you made some really significant changes to the code before merging this, and the new solution is something completely different than the one I uploaded. My example solves a different problem than the one that is now merged.
Some examples with l = [1, 2, 3, 4, 5, 6, 7]
My old solution:
split_list(l, 2) # [[1, 2, 3, 4], [5, 6, 7]]
split_list(l, 3) # [[1, 2, 3], [4, 5], [6, 7]]
split_list(l, 9) # [[1], [2], [3], [4], [5], [6], [7]]
The new solution:
chunk_into_n(l, 2) # [[1, 2, 3, 4], [5, 6, 7]]
chunk_into_n(l, 3) # [[1, 2, 3], [4, 5, 6], [7]]
chunk_into_n(l, 9) # [[1], [2], [3], [4], [5], [6], [7], [], []]
If you look at my old solution you see that the sub-lists differ at most 1 element from each other in length. This is to be desirable in cases where you perhaps want to split the list out into different thread workers and have the load spread as evenly as possible.
Secondly, the new solution will add empty elements if you try to split it into more than len(l)
elements, which is also not desirable behavior IMO.
Would it be OK if I try to submit my old solution again, or is it something you deem not useful?
@JonasAlfredsson the merged solution is far more common and more useful in most cases and also mirrors the one in the JS repo. We'll just stick to that one, no need to submit a different one, as the original version had some really hard-to-explain and quite frankly unexpected behavior.
Thank you for your response. That sounds reasonable, I just wanted to make sure you were aware of the differences in behavior of the two solutions.
Apologies if I sounded rude in my first comment, it was not my intention.
This code comes from one of my gists, but could probably be more useful here where it might be easier to find.