通过每次移动启动索引,将数组分为多个小阵列

发布于 2025-01-24 17:34:22 字数 461 浏览 3 评论 0原文

如何通过每次将数组启动位置转移一个小数组来得出多个小阵列?

示例输入数组

[1,2,3,4,5,6,7,8,9,10]

输出:大小3的多个子阵列,从i = 0,1,2,3 .....

[1,2,3] [2,3,4] [3,4,5] [4,5,6] [5,6,7] [6,7,8] [7,8,9] [8,9,10]

我可以通过编写自己的代码来做到这一点,

split(input){
 i =0
 results = []
 while(i<len(input)-3):
   start = i
   end = i+ 3 
   subarray = input[start:end]
   results.append(subarray)
} 
 return results

是否有python内置函数,或者sum lib?

How to derive multiple small arrays from a large array by shifting array start position by one each time?

Example input array

[1,2,3,4,5,6,7,8,9,10]

Output: Multiple subarrays of size 3, starting at i=0,1,2,3.....

[1,2,3]
[2,3,4]
[3,4,5]
[4,5,6]
[5,6,7]
[6,7,8]
[7,8,9]
[8,9,10]

I can do this by writing my own code

split(input){
 i =0
 results = []
 while(i<len(input)-3):
   start = i
   end = i+ 3 
   subarray = input[start:end]
   results.append(subarray)
} 
 return results

Is there a python inbuilt function for this, or sum lib?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

小镇女孩 2025-01-31 17:34:22

不在标准库中,但这是一个很棒的软件包:使用 more_itertools.windowed

l = [1,2,3,4,5,6,7,8,9,10]

# pip install more-itertools
from more_itertools import windowed
list(windowed(l, 3))

输出:

[(1, 2, 3),
 (2, 3, 4),
 (3, 4, 5),
 (4, 5, 6),
 (5, 6, 7),
 (6, 7, 8),
 (7, 8, 9),
 (8, 9, 10)]

或使用numpy

from numpy.lib.stride_tricks import sliding_window_view
sliding_window_view(l, 3).tolist()

输出:输出:

[[1, 2, 3],
 [2, 3, 4],
 [3, 4, 5],
 [4, 5, 6],
 [5, 6, 7],
 [6, 7, 8],
 [7, 8, 9],
 [8, 9, 10]]

Not in the standard library, but this is a great package: using more_itertools.windowed:

l = [1,2,3,4,5,6,7,8,9,10]

# pip install more-itertools
from more_itertools import windowed
list(windowed(l, 3))

output:

[(1, 2, 3),
 (2, 3, 4),
 (3, 4, 5),
 (4, 5, 6),
 (5, 6, 7),
 (6, 7, 8),
 (7, 8, 9),
 (8, 9, 10)]

Or with numpy:

from numpy.lib.stride_tricks import sliding_window_view
sliding_window_view(l, 3).tolist()

output:

[[1, 2, 3],
 [2, 3, 4],
 [3, 4, 5],
 [4, 5, 6],
 [5, 6, 7],
 [6, 7, 8],
 [7, 8, 9],
 [8, 9, 10]]
深爱不及久伴 2025-01-31 17:34:22

尝试ZIP

l = [1,2,3,4,5,6,7,8,9,10]
sublists = [list(zip(l,l[1:],l[2:]))]

>>> sublists
[[1, 2, 3],
 [2, 3, 4],
 [3, 4, 5],
 [4, 5, 6],
 [5, 6, 7],
 [6, 7, 8],
 [7, 8, 9],
 [8, 9, 10]]

Try with zip:

l = [1,2,3,4,5,6,7,8,9,10]
sublists = [list(zip(l,l[1:],l[2:]))]

>>> sublists
[[1, 2, 3],
 [2, 3, 4],
 [3, 4, 5],
 [4, 5, 6],
 [5, 6, 7],
 [6, 7, 8],
 [7, 8, 9],
 [8, 9, 10]]
嘿咻 2025-01-31 17:34:22

您可以使用列表理解和索引这样的列表来完成此操作:

k = 3
r = [1,2,3,4,5,6,7,8,9,10]
r_sublists = [r[i:i+k] for i in range(len(r) - k + 1)]

k是每个子列表中的项目数量。

You can do this with list comprehension and indexing like so:

k = 3
r = [1,2,3,4,5,6,7,8,9,10]
r_sublists = [r[i:i+k] for i in range(len(r) - k + 1)]

Where k is the number of items in each sub-list.

太傻旳人生 2025-01-31 17:34:22

您可以通过Numpy做到这一点:

import numpy as np

a = np.array([1,2,3,4,5,6,7,8,9,10])
np.lib.stride_tricks.sliding_window_view(a, 3)

# [[ 1  2  3]
#  [ 2  3  4]
#  [ 3  4  5]
#  [ 4  5  6]
#  [ 5  6  7]
#  [ 6  7  8]
#  [ 7  8  9]
#  [ 8  9 10]]

You can do this just by NumPy as:

import numpy as np

a = np.array([1,2,3,4,5,6,7,8,9,10])
np.lib.stride_tricks.sliding_window_view(a, 3)

# [[ 1  2  3]
#  [ 2  3  4]
#  [ 3  4  5]
#  [ 4  5  6]
#  [ 5  6  7]
#  [ 6  7  8]
#  [ 7  8  9]
#  [ 8  9 10]]
孤寂小茶 2025-01-31 17:34:22

这是您可以通过列表理解来完成的方式:

l = [1,2,3,4,5,6,7,8,9,10]
n = 3 # No of elements in each list
list_of_l = [l[i:i+n] for i in range(len(l)-n+1)]
list_of_l
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10]]

Here's how you can do it with list comprehension:

l = [1,2,3,4,5,6,7,8,9,10]
n = 3 # No of elements in each list
list_of_l = [l[i:i+n] for i in range(len(l)-n+1)]
list_of_l
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文