2016-07-26 45 views
-3

我是編碼的新手,並且試圖用Python編寫某些東西。以下是要求。將大陣列拆分爲具有重疊和特定長度的較小陣列

我有一個包含15個元素的數組。

A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] 

我想分成重疊長度7也就是說

l1 = [1,2,3,4,5,6,7] 
l2 = [2,3,4,5,6,7,8] 
l3 = [3,4,5,6,7,8,9] 
etc till 
l9 = [9,10,11,12,13,14,15] 

的小陣列如何才能做到這一點?

+6

所示的l1,l2 ... ln完成它的第一步是嘗試。 – Julien

+0

:) @Tonechas - 在這裏的所有答案,我應該感謝你,因爲這是適合我的要求最好的一個!不幸的是,我沒有足夠的積分來upvote你的答案..和stackoverflow不允許評論只是感謝一個答案.. – AntonyP

回答

0

你很可能使用該代碼:

A = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 
start = range(len(A) + 1) 
stop = start[7:] 
L = [A[i:j] for i, j in zip(start, stop)] 

演示:

In [340]: L 
Out[340]: 
[[1, 2, 3, 4, 5, 6, 7], 
[2, 3, 4, 5, 6, 7, 8], 
[3, 4, 5, 6, 7, 8, 9], 
[4, 5, 6, 7, 8, 9, 10], 
[5, 6, 7, 8, 9, 10, 11], 
[6, 7, 8, 9, 10, 11, 12], 
[7, 8, 9, 10, 11, 12, 13], 
[8, 9, 10, 11, 12, 13, 14], 
[9, 10, 11, 12, 13, 14, 15]] 

要訪問各列表中,你只需要指數L適當

In [341]: L[0] #first list 
Out[341]: [1, 2, 3, 4, 5, 6, 7] 

In [342]: L[1] #second list 
Out[342]: [2, 3, 4, 5, 6, 7, 8] 

In [343]: L[-1] #last list 
Out[343]: [9, 10, 11, 12, 13, 14, 15] 
1

你可以切片在for循環列表:

>>> A = list(range(1, 16)) 
>>> for i in range(len(A) - 6): 
...  print(A[i:i+7]) 
... 
[1, 2, 3, 4, 5, 6, 7] 
[2, 3, 4, 5, 6, 7, 8] 
[3, 4, 5, 6, 7, 8, 9] 
[4, 5, 6, 7, 8, 9, 10] 
[5, 6, 7, 8, 9, 10, 11] 
[6, 7, 8, 9, 10, 11, 12] 
[7, 8, 9, 10, 11, 12, 13] 
[8, 9, 10, 11, 12, 13, 14] 
[9, 10, 11, 12, 13, 14, 15] 

如果你想分配子列表將各個變量,它更好地使用列表理解:

L1, L2, ..., L9 = [A[i:i+7] for i in range(len(A) - 6)] 
+0

謝謝!但我怎樣才能把它變成像L1,L2等單個數組? – AntonyP

0

使用slice做到這一點。

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] 
# a = [1, 2, 3, 4, 5, 6, 7] 

LENGTH = 7 
result = [] 

if len(a) < LENGTH: 
    result.append(a[:]) 
else: 
    for i in range(len(a)-LENGTH+1): 
     result.append(a[i:i+LENGTH]) 

print result 
# [[1, 2, 3, 4, 5, 6, 7], [2, 3, 4, 5, 6, 7, 8], [3, 4, 5, 6, 7, 8, 9], [4, 5, 6, 7, 8, 9, 10], [5, 6, 7, 8, 9, 10, 11], [6, 7, 8, 9, 10, 11, 12], [7, 8, 9, 10, 11, 12, 13], [8, 9, 10, 11, 12, 13, 14], [9, 10, 11, 12, 13, 14, 15]] 
0

這個小功能應該做的正是你想要的,它也能處理,其中LEN(陣列)是不小數組的長度的整數倍的情況。當然,您可以輸入分割大小和重疊的擴展名。

def split_overlap(array,size,overlap): 
    result = [] 
    while True: 
     if len(array) <= size: 
      result.append(array) 
      return result 
     else: 
      result.append(array[:size]) 
      array = array[size-overlap:] 

如何使用它:

array = list(range(10)) 
print split_overlap(array,4,2) 
[[0, 1, 2, 3], [2, 3, 4, 5], [4, 5, 6, 7], [6, 7, 8, 9]] 

array = list(range(11)) 
print split_overlap(array,4,2) 
[[0, 1, 2, 3], [2, 3, 4, 5], [4, 5, 6, 7], [6, 7, 8, 9], [8, 9, 10]] 

您的具體情況:

A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] 
split_overlap(A,7,6) 

[[1, 2, 3, 4, 5, 6, 7], 
[2, 3, 4, 5, 6, 7, 8], 
[3, 4, 5, 6, 7, 8, 9], 
[4, 5, 6, 7, 8, 9, 10], 
[5, 6, 7, 8, 9, 10, 11], 
[6, 7, 8, 9, 10, 11, 12], 
[7, 8, 9, 10, 11, 12, 13], 
[8, 9, 10, 11, 12, 13, 14], 
[9, 10, 11, 12, 13, 14, 15]] 
0

它總是一個很好的做法,發佈自己的努力(代碼)解決問題。不過你可以試試我的解決方案,我希望它能幫助:

def overlap_split(array, split_length): 
    for i in range(len(array) - split_length): 
    print(array[i:i + (split_length + 1)]) 

如果你看看,我已經轉換@尤金的代碼,你可以重新使用一個功能非常密切you'ld通知。如果你打算利用分裂,那麼你將不得不將每個分裂存儲在單個陣列中,即,如通過@eugene