python - Comment diviser une liste en morceaux de taille uniforme

Mots clés : pythonlistsplitchunkspython

meilleur 5 Réponses python - Comment diviser une liste en morceaux de taille uniforme

vote vote

92

def chunks(lst, n):     """Yield successive n-sized chunks from lst."""     for i in range(0, len(lst), n):         yield lst[i:i + n] 
import pprint pprint.pprint(list(chunks(range(10, 75), 10))) [[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],  [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],  [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],  [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],  [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],  [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],  [70, 71, 72, 73, 74]] 
def chunks(lst, n):     """Yield successive n-sized chunks from lst."""     for i in xrange(0, len(lst), n):         yield lst[i:i + n] 
[lst[i:i + n] for i in range(0, len(lst), n)] 
[lst[i:i + n] for i in xrange(0, len(lst), n)] 
vote vote

80

def chunks(l, n):     n = max(1, n)     return (l[i:i+n] for i in range(0, len(l), n)) 
vote vote

76

import numpy as np  lst = range(50) np.array_split(lst, 5) # [array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), #  array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19]), #  array([20, 21, 22, 23, 24, 25, 26, 27, 28, 29]), #  array([30, 31, 32, 33, 34, 35, 36, 37, 38, 39]), #  array([40, 41, 42, 43, 44, 45, 46, 47, 48, 49])] 
vote vote

70

from itertools import izip, chain, repeat  def grouper(n, iterable, padvalue=None):     "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"     return izip(*[chain(iterable, repeat(padvalue, n-1))]*n) 
#from itertools import izip_longest as zip_longest # for Python 2.x from itertools import zip_longest # for Python 3.x #from six.moves import zip_longest # for both (uses the six compat library)  def grouper(n, iterable, padvalue=None):     "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"     return zip_longest(*[iter(iterable)]*n, fillvalue=padvalue) 
vote vote

50

from itertools import islice  def chunk(it, size):     it = iter(it)     return iter(lambda: tuple(islice(it, size)), ()) 
>>> list(chunk(range(14), 3)) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13)] 
from itertools import islice, chain, repeat  def chunk_pad(it, size, padval=None):     it = chain(iter(it), repeat(padval))     return iter(lambda: tuple(islice(it, size)), (padval,) * size) 
>>> list(chunk_pad(range(14), 3)) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, None)] >>> list(chunk_pad(range(14), 3, 'a')) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 'a')] 
_no_padding = object()  def chunk(it, size, padval=_no_padding):     if padval == _no_padding:         it = iter(it)         sentinel = ()     else:         it = chain(iter(it), repeat(padval))         sentinel = (padval,) * size     return iter(lambda: tuple(islice(it, size)), sentinel) 
>>> list(chunk(range(14), 3)) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13)] >>> list(chunk(range(14), 3, None)) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, None)] >>> list(chunk(range(14), 3, 'a')) [(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11), (12, 13, 'a')] 
_no_padding = object() def chunk(it, size, padval=_no_padding):     it = iter(it)     chunker = iter(lambda: tuple(islice(it, size)), ())     if padval == _no_padding:         yield from chunker     else:         for ch in chunker:             yield ch if len(ch) == size else ch + (padval,) * (size - len(ch)) 
>>> list(chunk([1, 2, (), (), 5], 2)) [(1, 2), ((), ()), (5,)] >>> list(chunk([1, 2, None, None, 5], 2, None)) [(1, 2), (None, None), (5, None)] 

Questions similaires