0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

配列を指定単位で区切る方法

Last updated at Posted at 2015-11-09

配列を指定単位で区切る方法

大量のデータをloopで、n件単位でコミットしたい時ってありますよね?
そういう時に有用かなと

記載している内容は、dbとか一切関係ないです

おまけ:Pythonの方の記述で、あまりがNoneならないようにスマートに記述したいです。

  • php5.x
main.php
<?php

$input = array(0,1,2,3,4,5,6,7,8,9);
$unit = 3;
print_r(array_chunk($input, $unit));

/*
Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 1
            [2] => 2
        )

    [1] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
        )

    [2] => Array
        (
            [0] => 6
            [1] => 7
            [2] => 8
        )

    [3] => Array
        (
            [0] => 9
        )
)
*/
  • Python3
main.py
# -*- coding: utf-8 -*-
import itertools
unit = 3
for x in itertools.zip_longest(*[iter(range(10))] * unit):
	print(x)
"""
(0, 1, 2)
(3, 4, 5)
(6, 7, 8)
(9, None, None)
"""
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?