Hướng dẫn dùng list split python
list, split, join trong PythonHôm nay ta sẽ học cách chuyển từ chuỗi(string) sang list và ngược lại. Show Ta muốn chuyển từ string sang list.
list(my_string): sẽ chuyển chuỗi sang một list những ký tự. Còn Ta muốn chuyển 1 chuỗi sang các từ (word) ta dùng split().
Còn nếu muốn chuyển 1 list sang chuỗi ta dùng join().
Như vậy ta đã biết cách dùng list, split and join rồi đó. Easy easy ^_^. list, split, join trong PythonHôm nay ta sẽ học cách chuyển từ chuỗi(string) sang list và ngược lại. Nội dung chính
Ta muốn chuyển từ string sang list.
list(my_string): sẽ chuyển chuỗi sang một list những ký tự. Còn Ta muốn chuyển 1 chuỗi sang các từ (word) ta dùng split().
Còn nếu muốn chuyển 1 list sang chuỗi ta dùng join().
Như vậy ta đã biết cách dùng list, split and join rồi đó. Easy easy ^_^. How do you split a list into evenly sized chunks?"Evenly sized chunks", to me, implies that they are all the same length, or barring that option, at minimal variance in length. E.g. 5 baskets for 21 items could have the following results:
A practical reason to prefer the latter result: if you were using these functions to distribute work, you've built-in the prospect of one likely finishing well before the others, so it would sit around doing nothing while the others continued working hard. Critique of other answers hereWhen I originally wrote this answer, none of the other answers were evenly sized chunks - they all leave a runt chunk at the end, so they're not well balanced, and have a higher than necessary variance of lengths. For example, the current top answer ends with:
Others, like Why can't we divide these better? Cycle SolutionA high-level balanced solution using
Now we need our lists into which to populate the elements:
Finally, we zip the elements we're going to allocate together with a cycle of the baskets until we run out of elements, which, semantically, it exactly what we want:
Here's the result:
To productionize this solution, we write a function, and provide the type annotations:
In the above, we take our list of items, and the max number of baskets. We create a list of empty lists, in which to append each element, in a round-robin style. SlicesAnother elegant solution is to use slices - specifically the less-commonly used step argument to slices. i.e.:
This is especially elegant in that slices don't care how long the data are - the result, our first basket, is only as long as it needs to be. We'll only need to increment the starting point for each basket. In fact this could be a one-liner, but we'll go multiline for readability and to avoid an overlong line of code:
And I don't expect most use-cases to benefit very much, as the original data is already fully materialized in a list, but for large datasets, it could save nearly half the memory usage.
View results with:
Updated prior solutionsHere's another balanced solution, adapted from a function I've used in production in the past, that uses the modulo operator:
And I created a generator that does the same if you put it into a list:
And finally, since I see that all of the above functions return elements in a contiguous order (as they were given):
OutputTo test them out:
Which prints out:
Notice that the contiguous generator provide chunks in the same length patterns as the other two, but the items are all in order, and they are as evenly divided as one may divide a list of discrete elements. |