python相当于clojure的分区 - 全部?

时间:2011-02-26 19:06:40

标签: python clojure

在python的标准库中寻找一些东西或语法技巧。

对于非clojure程序员,partition-all应具有以下语义:

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

假设lein(lst)== 60

5 个答案:

答案 0 :(得分:6)

Python中没有这样的功能。你可以这样做:

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

答案 1 :(得分:3)

range内置函数添加第三个“步长”参数让你非常接近:

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

您可以从那里为上限和下限创建元组:

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

如果需要,可以创建实际范围:

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

当然,如果需要,你可以将0,60和16参数化为你自己的函数。

答案 2 :(得分:1)

我认为标准库中没有类似partition_all的函数,所以我担心你自己编写。但是看https://github.com/clojure/clojure/blob/b578c69d7480f621841ebcafdfa98e33fcb765f6/src/clj/clojure/core.clj#L5599我认为你可以用Python实现它:

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

答案 3 :(得分:1)

recipes section of the itertools documentation具有grouper功能,可满足您的需求。

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

请注意,还有一个“分区”的秘诀,但这会有所不同。

答案 4 :(得分:0)

我相信我有一个较短的答案,不需要导入任何库。

以下是您的单行:

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

现在作为一个函数(假设您使用列表):

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

编辑:基于Triptych答案的新解决方案:

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