numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]
output = [[], [], []]
for number in numbers:
output[(number+2)%3].append(number)
print(output)
When you run the source code of , the remaining values 0
, 1
, and 2
are displayed in a bundled state.
[1, 4, 7], [2, 5, 8], [3, 6, 9]
In this state, I would like to arrange in the order of [1, 2, 3], [4, 5, 6], [7, 8, 9]
.
If possible, I would like to know how to use the sort()
function.
There is no purpose or reason for the question, so it can only be inferred peripherally.
flattern -> sort -> slice
sample.
import itertools as it
L = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
flatten_L = sorted(it.chain(*L)) # [1, 2, 3, 4, 5, 6, 7, 8, 9]
_iter = iter(flatten_L)
list(iter(lambda: list(it.islice(_iter, 3)), [])) # [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
© 2024 OneMinuteCode. All rights reserved.