简体   繁体   English

如何在不使用循环的情况下按索引添加 numpy 数组变量

[英]How to add numpy array variables by index without using loops

I have this problem where I would prefer not using loops because I am working with a big data as a solution:我有这个问题,我不想使用循环,因为我正在使用大数据作为解决方案:

This is what I am trying to do: (I know this works to [6 6 6], but I want to "join" it by index)这就是我想要做的:(我知道这适用于 [6 6 6],但我想通过索引“加入”它)

import numpy as np

np_1 = np.asarray([1,1,1])
np_2 = np.asarray([2,2,2])
np_3 = np.asarray([3,3,3])

np_4 = np_1 + np_2 + np_3
# np_4 should be [1,2,3,1,2,3,1,2,3]

Are there ways to do this?有没有办法做到这一点? or should I look for options outside of numpy?还是我应该寻找 numpy 之外的选项?

You can try the following method:您可以尝试以下方法:

np.ravel(np.stack(np_1,np_2,np3),'F')

One way to do it is to stack the sequences depth-wise and flatten it:一种方法是深度堆叠序列并将其展平:

np.dstack([np_1, np_2, np_3]).flatten()

Try this:尝试这个:

np.array([np_1, np_2, np_3]).transpose().flatten()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用 python 循环的情况下将元素插入 3d numpy 数组中的特定索引? - How to insert element to a specific index in 3d numpy array without using python loops? 如何在不使用嵌套 for 循环的情况下根据其索引值划分 2d numpy 数组中的每个元素? - How to divide each element in a 2d numpy array in relation to its index value without the use of nested for loops? 如何使用循环反转 numpy 数组 - How to reverse a numpy array using loops 使用另一个数组作为没有循环的参考来更改 numpy 数组的值 - Changing values of a numpy array using another array as a reference without loops 使用没有循环的numpy堆叠2D数组中的每2个数组 - Stack every 2 arrays in 2D array using numpy without loops 使用生成器填充/创建没有 for 循环的 numpy 数组 - Using generators to fill/create a numpy array without for loops 如何在不重复的情况下使用迭代循环用不同的数字填充numpy数组? - How to fill numpy array with different numbers using iterative loops without duplication in python? 如何在不使用循环的情况下为3D numpy数组中的每个值进行N个随机选择 - How to make N random choices for each value in a 3D numpy array without using loops 如何在不使用显式循环的情况下根据条件替换 numpy.ndarray 的“数组元素”? - How to replace "array elements" of a numpy.ndarray with respect to a condition without using explicit loops? 如何在不使用numpy数组中的两个For循环的情况下提高时间复杂度,优化结构? - How to improve time complexity without using two For loops in numpy array, optimize the structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM