Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python equivalent to clojure's partition-all?

Tags:

python

clojure

looking for something in python's standard lib or a syntax trick.

for non-clojure programmers, partition-all should have these semantics:

partition_all(16, lst) == [lst[0:16], lst[16:32], lst[32:48], lst[48:60]]

assuming len(lst) == 60

like image 954
Ellery Newcomer Avatar asked Feb 26 '11 19:02

Ellery Newcomer


4 Answers

There is no such function in Python. You can do this:

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]]
like image 157
Jochen Ritzel Avatar answered Nov 04 '22 13:11

Jochen Ritzel


Adding a third "step size" parameter to the range built-in function gets you pretty close:

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

You can create tuples for the upper and lower bounds from there:

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

Or create the actual ranges if you need them:

>>> [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]]

Naturally you can parameterize the 0, 60, and 16 into your own function if you need to.

like image 32
Kenan Banks Avatar answered Nov 04 '22 14:11

Kenan Banks


I don't think there is a partition_all like function in the standard library so I'm afraid you are writing your own. However looking at https://github.com/clojure/clojure/blob/b578c69d7480f621841ebcafdfa98e33fcb765f6/src/clj/clojure/core.clj#L5599 I'm thinking you could implement it in Python like this:

>>> 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]
>>>
like image 1
stderr Avatar answered Nov 04 '22 15:11

stderr


The recipes section of the itertools documentation has a grouper function that does what you want.

from itertools import zip_longest
def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)

Beware that there's also a recipe for "partition", but that does something different.

like image 1
Brennan Avatar answered Nov 04 '22 13:11

Brennan