在python的標準庫或語法技巧中尋找東西。python相當於clojure的分區 - 所有?
非Clojure的程序員,分區都應該有這些語義:
partition_all(16, lst) == [lst[0:16], lst[16:32], lst[32:48], lst[48:60]]
假設LEN(LST)== 60
在python的標準庫或語法技巧中尋找東西。python相當於clojure的分區 - 所有?
非Clojure的程序員,分區都應該有這些語義:
partition_all(16, lst) == [lst[0:16], lst[16:32], lst[32:48], lst[48:60]]
假設LEN(LST)== 60
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]]
添加第三個 「步長」 參數到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到你自己的功能,如果你需要。
認爲這有效,但請不要讓我寫我自己的功能或寫16在兩個地方 – 2011-02-26 19:29:40
我猜你直到最後都沒有讀書。習慣於編寫自己的函數,如果你想成爲程序員,Newcomer。 – Triptych 2011-03-04 12:06:31
我不認爲有一個像標準庫,所以我怕你編寫自己的功能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]
>>>
我相信我有一個較短的答案不需要導入任何庫。
這裏是一個班輪爲您提供:
>>> 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))
[]
編輯:新的解決方案基於三聯的回答:
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))
[]
>>>
請指出任何錯誤,因爲它們似乎對我而言不可見。 – 2011-03-03 20:33:15
@Ellery,我犯了一個錯誤。我的錯。 – 2011-03-03 20:49:51
你的簡短答案不需要什麼? – 2011-03-03 21:02:15
@Ellery Newcomer:我看到了,我改變了答案。 – 2011-02-26 20:45:15