我是编码的新手,我正在尝试用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]
如何做到这一点?
答案 0 :(得分: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)]
答案 1 :(得分:1)
这个小函数应该完全符合你的要求,它也处理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]]
答案 2 :(得分: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]
答案 3 :(得分: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]]
答案 4 :(得分:0)
在解决问题时发布自己的努力(代码)总是一种很好的做法。但是你可以尝试我的解决方案,我希望它有所帮助:
def overlap_split(array, split_length):
for i in range(len(array) - split_length):
print(array[i:i + (split_length + 1)])
如果仔细观察,您会注意到我已将@ eugene的代码转换为您可以重复使用的功能。如果您打算使用拆分,则必须将每个拆分存储在单个数组中,即l1,l2 ... ln,如@eugene所示