元素循环范围的数值变化值

发布于 2025-02-09 23:47:08 字数 348 浏览 4 评论 0原文

为简单起见,我有一系列元素:
阵列([0,1,2,3,4,5,6,7,7,8,9,10,11,11,12,13,14,14,15,16,17,18,19])
从0,每个nth元素开始,我想更改下一个M元素的值。
例如:n = 5; m = 2,我想将元素设置为100。

输出应该是:
阵列([100,100,2,3,4,100,100,100,7,8,9,100,100,100,100,12,13,14,100,100,100,17,18,19])

这是在一行(带指数)中进行的努力方法吗?还是仅通过循环或列表理解 - 哪个会慢。

I have an array of elements, for simplicity:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]).
Starting with 0, every nth element, I'd like to change the value of the next m elements.
For example: n=5; m=2 and I'd like to set the elements to 100.

The output should be something like:
array([ 100, 100, 2, 3, 4, 100, 100, 7, 8, 9, 100, 100, 12, 13, 14, 100, 100, 17, 18, 19])

is this a numpy way to do it in one line (with indices)? or only by looping or list comprehension - which will be slower..?

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

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

发布评论

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

评论(1

云裳 2025-02-16 23:47:08

以下可以完成这项工作

arr.reshape((-1, n))[:, :m] = 100

The following could do the job

arr.reshape((-1, n))[:, :m] = 100
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文