Découper une liste en plusieurs partie égalLa fonction:(Python):
def splitlist( iterable, start=0, step=30, end=0 ):
"""Return a list containing an slice of iterable.
start (!) defaults to 0.; step is split index, (!) defaults to 30.; end (!) defaults to 0.
For example, splitlist(range(4)) returns [[0, 1, 2, 3]].
"""
try:
splited = []
if end <= 0:
end = len( iterable )
for index in xrange( step, end, step ):
splited.append( iterable[ start:index ] )
start = index
splited.append( iterable[ start:end ] )
return splited
except:
from traceback import print_exc
print_exc()
return [ iterable ]
Un exemple:(Python):
import string
maliste = list( string.printable )
print maliste
print
# splitlist( iterable, start=0, step=30, end=0 ) )
splited = splitlist( maliste )
print "Nombre total d'items", len( maliste )
print "Nombre de listes", len( splited )
for count, liste in enumerate( splited ):
print "liste #", count+1
print len( liste ), liste
print
"""
>>>
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':',
';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~', ' ', '\t', '\n', '\r', '\x0b', '\x0c']
Nombre total d'items 100
Nombre de listes 4
liste # 1
30 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't']
liste # 2
30 ['u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X']
liste # 3
30 ['Y', 'Z', '!', '"', '#', '$', '%', '&', "'", '(', ')', '*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`']
liste # 4
10 ['{', '|', '}', '~', ' ', '\t', '\n', '\r', '\x0b', '\x0c']
>>>
"""
Après 1 heure de recherche sur le web avec aucun résultat valable. J'ai créé cette fonction made in Frost

si vous avez plus simple merci de mettre votre Snippet