2011-02-26 9 views

Respuesta

6

No existe tal función en Python. Usted puede hacer esto:

from itertools import islice 
def chunkwise(n, iterable): 
    it = iter(iterable) 
    while True: 
     chunk = list(islice(it, n)) 
     if not chunk: 
      break 
     yield chunk 

print list(chunkwise(3, range(10))) 
# [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]] 
+0

@Ellery Recién llegado: Veo y cambié la respuesta. –

3

Adición de un tercer parámetro "tamaño de paso" a la range función incorporada te acerca bastante:

>>> range(0,60,16) 
[0, 16, 32, 48] 

Puedes crear tuplas para los límites superior e inferior desde allí:

>>> [(i, i+16) for i in range(0, 60, 16)] 
[(0, 16), (16, 32), (32, 48), (48, 64)] 

O crear los rangos reales si los necesita:

>>> [range(i, i+16) for i in range(0, 60, 16)] 
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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]] 

Naturalmente posibilidad de parametrizar el 0, 60 y 16 en su propia función, si es necesario.

+1

creo que esto funciona, pero por favor no me haga escribir mi propia función o escribir 16 en dos lugares –

+0

Supongo que no te molestaste en leer hasta el final. Acostúmbrate a escribir tus propias funciones si quieres ser un programador, Recién llegado. – Triptych

0

No creo que hay una partition_all como la función de la biblioteca estándar así que me temo que está escribiendo su propio. Sin embargo mirando https://github.com/clojure/clojure/blob/b578c69d7480f621841ebcafdfa98e33fcb765f6/src/clj/clojure/core.clj#L5599 estoy pensando que podría ponerlo en práctica en Python como esto:

>>> from itertools import islice 
>>> lst = range(60) 
>>> def partition_all(n, lst, step=None, start=0): 
...  step = step if step is not None else n 
...  yield islice(lst, start, n) 
...  while n < len(lst): 
...    start, n = start + step, n + step 
...    yield islice(lst, start, n) 
... 
>>> 
>>> for partition in partition_all(16, lst): 
...  l = list(partition) 
...  print len(l), l 
... 
16 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 
16 [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] 
16 [32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47] 
12 [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59] 
>>> 
0

Creo que tengo una respuesta más corto, que no requiere ninguna biblioteca de importación.

Aquí es una sola línea para usted:

>>> lst = list(range(60)) 
>>> [lst[i * 16: (i + 1) * 16] for i in range(len(lst)/size + int((len(lst) % 16) > 0))) 
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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]] 

ahora como una función (supone que se trabaja con una lista):

def partition(lst, size): 
    assert(size >= 0) 
    if not lst: return() 
    return (lst[i * size: (i + 1) * size] for i in range(len(lst)/size + int((len(lst) % size) > 0))) 
>>> partition(list(range(78)), 17) 
<generator object <genexpr> at 0x7f284e33d5a0> 
>>> list(partition(list(range(78)), 17)) 
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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, 75, 76, 77]] 
>>> list(partition(range(16), 4)) 
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] 
>>> print list(partition(range(0), 4)) 
[] 

EDIT: Nueva solución basada en el tríptico de respuesta:

def partition(lst, size): 
    assert(size >= 0) 
    if not lst: return() 
    return (lst[i: i + size] for i in range(0, len(lst), size)) 

>>> partition(list(range(78)), 17) 
<generator object <genexpr> at 0x025F5A58> 
>>> list(partition(list(range(78)), 17)) 
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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, 75, 76, 77]] 
>>> list(partition(list(range(16)), 17)) 
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]] 
>>> list(partition(list(range(16)), 4)) 
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] 
>>> list(partition([], 17)) 
[] 
>>> 
+0

Por favor, señale cualquier error, ya que parecen ser invisibles para mí. –

+0

@Ellery, he cometido un error. Mi error. –

+0

¿Qué es lo que su respuesta más corta no requiere? –

Cuestiones relacionadas