在本文中,我们将详细介绍NumPy使用索引数组将一个数组存储在另一个数组中的各个方面,并为您提供关于numpy数组索引的相关解答,同时,我们也将为您带来关于2个数组相加,将一个数组写入另一个数组、c#
在本文中,我们将详细介绍NumPy使用索引数组将一个数组存储在另一个数组中的各个方面,并为您提供关于numpy 数组索引的相关解答,同时,我们也将为您带来关于2个数组相加,将一个数组写入另一个数组、c# – 在另一个数组中找到一个数组(byte [])?、C++程序将一个数组推入另一个数组中、C++程序:将一个数组的所有元素复制到另一个数组中的有用知识。
本文目录一览:- NumPy使用索引数组将一个数组存储在另一个数组中(numpy 数组索引)
- 2个数组相加,将一个数组写入另一个数组
- c# – 在另一个数组中找到一个数组(byte [])?
- C++程序将一个数组推入另一个数组中
- C++程序:将一个数组的所有元素复制到另一个数组中
NumPy使用索引数组将一个数组存储在另一个数组中(numpy 数组索引)
我的问题是有关我想使用numpy表示的特定数组操作。
我有一个浮点数组w
和一个idx
与长度相同的索引数组,w
我想将所有w
具有相同idx
值的值相加并将它们收集在一个数组中v
。作为一个循环,它看起来像这样:
for i,x in enumerate(w):
v[idx[i]] += x
有没有办法用数组操作做到这一点?我的猜测是,v[idx] += w
但这不起作用,因为idx
多次包含相同的索引。
谢谢!
2个数组相加,将一个数组写入另一个数组
已知数组a和数组b:
把数组b的结果加入数组a,同cust_no,lotno对应的part_count累加。变成这样的结果:
这样如何做到?需要用哪些数组函数?谢谢!
回复讨论(解决方案)
$v[''cust_no''].''_''.$v[''lotno'']这句是什么意思?
将 cust_no 和 lotno的值组成键判断重复用的
将 cust_no 和 lotno的值组成键判断重复用的
哦,原来可以这样用啊。谢谢了!
c# – 在另一个数组中找到一个数组(byte [])?
注意:我用[c#]搜索并没有找到任何东西,我感到很惊讶.
解决方法
static int search(byte[] haystack,byte[] needle) { for (int i = 0; i <= haystack.Length - needle.Length; i++) { if (match(haystack,needle,i)) { return i; } } return -1; } static bool match(byte[] haystack,byte[] needle,int start) { if (needle.Length + start > haystack.Length) { return false; } else { for (int i = 0; i < needle.Length; i++) { if (needle[i] != haystack[i + start]) { return false; } } return true; } }
C++程序将一个数组推入另一个数组中
A linear sequential data structure called an array is used to store homogeneous data in a series of memory regions. An array needs to have certain features to insert, delete, traverse, and update elements effectively, just like other data structures do. Our arrays in C++ are static. In addition, C++ offers a few dynamic array structures. There may be a maximum of Z elements that can be stored inside a static array. And there are currently n elements in it. In this article, we will see how to push the elements of one array inside another array in C++.
理解概念并举例说明
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] Another given array B = [56, 42, 15, 18, 20, 32] Pushing the elements of B into A, signifies that A becomes: A = [10, 14, 65, 85, 96, 12, 35, 74, 69, 56, 42, 15, 18, 20, 32]
在上面的示例中,很明显我们有两个数组A和B。将B推入A意味着将B中的所有元素插入到数组A中。这些元素将被添加到A的末尾。但是要实现这一点,我们必须检查一件事情,即A中剩余的空位(即A的最大大小减去A中现有元素的数量)是否与B中的元素数量相同或更大。否则,我们无法将它们推入A中。让我们看一下算法以及C++实现代码。
Algorithm
take the array A and B as input, the number of elements n in A as input, the number of elements m in B as input
如果 A 有足够的空间来容纳整个 B,则
立即学习“C++免费学习笔记(深入)”;
for each element e in B, do
append e to array A
结束循环
end if
return array A and new size n
Example
#include <iostream> # define Z 50 using namespace std; void displayArr(int arr[], int n){ for( int i = 0; i < n; i++ ){ cout << arr[ i ] << ", "; } cout << endl; } void insertAtEnd( int arr[], int &n, int e ){ if( n < Z ) { arr[ n ] = e; } n = n + 1; } void pushArrayToAnother( int A[], int &n, int B[], int m ) { if( (Z - n) >= m ){ for( int i = 0; i < m; i++ ) { insertAtEnd( A, n, B[i] ); } } } int main() { int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; int n = 12; int B[ Z ] = {56, 84, 23, 12, 17, 19, 65, 32}; int m = 8; cout << "First Array: "; displayArr( A, n ); cout << "Second Array: "; displayArr( B, m ); pushArrayToAnother( A, n, B, m ); cout << "Array A after pushing B:" << endl; displayArr( A, n ); }
输出
First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Second Array: 56, 84, 23, 12, 17, 19, 65, 32, Array A after pushing B: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,
Using Dynamic Arrays or Vectors
相同的事情可以使用向量来完成。向量是C++ STL中存在的动态数组。如果我们考虑使用向量,我们就不需要关心插入元素时的可用空间。因为向量是动态的,它会在需要时自动添加新的插槽。算法与可用插槽检查相同。
Algorithm
take the array A and B as input, the number of elements n in A as input, the number of elements m in B as input
for each element e in B, do
append e to array A
结束循环
return array A and new size n
Example
#include <iostream> #include <vector> # define Z 50 using namespace std; void displayArr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ) { cout << v[ i ] << ", "; } cout << endl; } void pushArrayToAnother( vector<int> &A, vector<int> B ){ for( int i = 0; i < B.size() ; i++ ) { A.push_back( B[i] ); } } int main(){ vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; vector<int> B = {56, 84, 23, 12, 17, 19, 65, 32}; cout << "First Array: "; displayArr( A ); cout << "Second Array: "; displayArr( B ); pushArrayToAnother( A, B ); cout << "Array A after pushing B:" << endl; displayArr( A ); }
输出
First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Second Array: 56, 84, 23, 12, 17, 19, 65, 32, Array A after pushing B: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,
在向量中使用insert()函数
之前的方法是一个手动的过程。然而,我们可以使用vector STL中的insert()函数来实现相同的功能。insert()函数接受一个位置指针(使用迭代器)和一个迭代器,从一个容器对象中复制元素并将其从位置索引插入到另一个容器对象中。让我们看一下C++的实现以获得清晰的视图。
Example
#include <iostream> #include <vector> # define Z 50 using namespace std; void displayArr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << ", "; } cout << endl; } void pushArrayToAnother( vector<int> &A, vector<int> B ) { A.insert( A.end(), B.begin(), B.end() ); } int main() { vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; vector<int> B = {56, 84, 23, 12, 17, 19, 65, 32}; cout << "First Array: "; displayArr( A ); cout << "Second Array: "; displayArr( B ); pushArrayToAnother( A, B ); cout << "Array A after pushing B:" << endl; displayArr( A ); }
输出
First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Second Array: 56, 84, 23, 12, 17, 19, 65, 32, Array A after pushing B: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,
Conclusion
在本文中,我们看到了几种不同的方法来将一个数组元素插入或推送到另一个数组的末尾。在第一个示例中,我们使用简单的C++数组,需要特别注意静态数组中可用空间的情况。在接下来的两种方法中,我们不需要关心这一点,因为我们使用的是动态的向量,它会在需要时自动分配空间。
以上就是C++程序将一个数组推入另一个数组中的详细内容,更多请关注php中文网其它相关文章!
C++程序:将一个数组的所有元素复制到另一个数组中
数组数据结构用于在连续的内存中存储同质数据 位置以顺序方式访问它们。数组是线性数据结构,因此 数组的基本操作可以在线性时间内执行。在本文中,我们将了解如何 在 C++ 中将一个数组中的元素复制到另一个新数组。
由于数组元素是同类的,因此新数组将具有相同的类型。创建后 另一个相同大小的数组,我们只需将第一个数组中的元素复制到第二个数组 一个。让我们看看算法和 C++ 实现,以便更好地理解。
算法
- 读取数组 A 及其大小 n 作为输入
- 创建大小与 A 相同的空数组 B,即 n
- 对于 i 的范围从 0 到 n-1,执行
- B[ i ] := A[ i ]i>
- 结束
示例
#include <iostream> using namespace std; void display( int arr[], int n ){ for ( int i = 0; i < n; i++ ) { cout << arr[i] << ", "; } } void solve( int arr[], int newArr[], int n ){ int i; for ( i = 0; i < n; i++ ) { newArr[ i ] = arr [ i ]; } } int main(){ int arr[] = {9, 15, 24, 28, 20, 6, 12, 78, 2, 12, 78, 44, 25, 115, 255, 14, 96, 84 }; int n = sizeof( arr ) / sizeof( arr[0] ); cout << "Given array: "; display(arr, n); int newArray[n] = {0}; solve( arr, newArray, n ); cout << "\nArray After copying: "; display(newArray, n); }
输出
Given array: 9, 15, 24, 28, 20, 6, 12, 78, 2, 12, 78, 44, 25, 115, 255, 14, 96, 84, Array After copying: 9, 15, 24, 28, 20, 6, 12, 78, 2, 12, 78, 44, 25, 115, 255, 14, 96, 84,
结论
从一个数组复制元素是基于数组的编程中最简单的任务之一。 我们创建一个新数组,其大小至少等于给定数组的大小。然后我们遍历 遍历给定数组的每个索引,然后将给定数组中的元素复制到新数组 大批。由于不需要多次遍历数组,因此操作可以是 在线性时间内执行,因此渐近上限为 O(n)。对于空间的利用也是如此 新数组需要相同数量的空间。复制需要 O(n) 空间量 元素添加到新数组。
以上就是C++程序:将一个数组的所有元素复制到另一个数组中的详细内容,更多请关注php中文网其它相关文章!
今天关于NumPy使用索引数组将一个数组存储在另一个数组中和numpy 数组索引的介绍到此结束,谢谢您的阅读,有关2个数组相加,将一个数组写入另一个数组、c# – 在另一个数组中找到一个数组(byte [])?、C++程序将一个数组推入另一个数组中、C++程序:将一个数组的所有元素复制到另一个数组中等更多相关知识的信息可以在本站进行查询。
本文标签: