1
0

More than 1 year has passed since last update.

python2 中类似ruby each_slice的实现

Posted at
def each_slice(arr, n):
    return [arr[i:i + n] for i in range(0, len(arr), n)]

print(each_slice([1, 2, 3, 4, 5], 2))
print(each_slice([1, 2, 3, 4, 5], 3))
print(each_slice([1, 2, 3, 4, 5], 4))
# => [[1, 2], [3, 4], [5]]
# => [[1, 2, 3], [4, 5]]
# => [[1, 2, 3, 4], [5]]

参考:

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0