交叉连接 2 个向量的元素以产生第三个
Cross Concatenate Elements of 2 Vectors to Produce a Third
我有 2 个向量,并希望将一个向量分布在另一个向量上以形成第三个向量,如下所示:
V1 = (a,b,c)
V2 = (d,e,f)
结果:
V3 = (ad,ae,af,bd,be,bf,...cf) 'nine total elements
我知道该怎么做的唯一方法是循环。我尝试了多种不同的搜索方式,但找不到"一行代码"解决方案,以避免循环。
如果我错过了,请指出我。我可能没有找到正确的搜索参数。
如果不可能,请饶恕我的痛苦,并让我知道:,,,(。
如果有答案,请分享。
您不清楚ab
操作的含义。我假设你想在这里乘以两个实数。
在 Python 中,你可以使用理解。这里有一个完整的代码片段。
v1 = (2, 3, 5)
v2 = (7, 11, 13)
v3 = tuple(x * y for x in v1 for y in v2)
然后,v3
的值为
(14, 22, 26, 21, 33, 39, 35, 55, 65)
如愿以偿。如果你想要一个Python列表,代码看起来更容易:使用
v3 = [x * y for x in v1 for y in v2]
如何将操作更改为串联或其他任何所需的操作将很明显。 下面是字符串串联的示例代码:
v1 = ('a', 'b', 'c')
v2 = ('d', 'e', 'f')
v3 = tuple(x + y for x in v1 for y in v2)
这导致
('ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf')
您也可以使用 itertools
模块中的product()
(我在此答案的第一个版本中使用了该模块(,但上述内容似乎更容易。
在 R 中:
as.vector(sapply(V1, function(x) paste0(x, V2)))
在C++:
std::vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
。不过,我通常会将其格式化为多行。
作为MCVE:
#include <iostream>
#include <vector>
using namespace std;
template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
const char *sep = "{ ";
for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
return out << " }";
}
int main()
{
vector<double> v1{ 1.0, 2.0, 3.0 }, v2{ 4.0, 5.0, 6.0 };
// in one line:
vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
// output:
cout << v3 << endl;
// done
return 0;
}
输出:
{ 4, 5, 6, 8, 10, 12, 12, 15, 18 }
(在 ideone 上测试。
谢谢,罗里·道尔顿,给你的启发。在C++中,我也可以这样做:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string operator*(const string &str1, const string &str2)
{
return str1 + str2;
}
template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
const char *sep = "{ ";
for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
return out << " }";
}
int main()
{
vector<string> v1{ "a", "b", "c" }, v2{ "d", "e", "f" };
// in one line:
vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
// output:
cout << v3 << endl;
// done
return 0;
}
输出:
{ ad, ae, af, bd, be, bf, cd, ce, cf }
经过一点改进,它甚至可以适用于混合类型:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string operator*(const string &arg1, int arg2)
{
string ret; for (int i = 0; i < arg2; ++i) ret += arg1;
return ret;
}
template <typename ELEM>
ostream& operator << (ostream &out, const vector<ELEM> &vec)
{
const char *sep = "{ ";
for (const ELEM &elem : vec) { out << sep << elem; sep = ", "; }
return out << " }";
}
int main()
{
vector<string> v1{ "a", "b", "c" }; vector<int> v2{ 1, 2, 3 };
// in one line:
vector<decltype(v1.front() * v2.front())> v3 { v1[0] * v2[0], v1[0] * v2[1], v1[0] * v2[2], v1[1] * v2[0], v1[1] * v2[1], v1[1] * v2[2], v1[2] * v2[0], v1[2] * v2[1], v1[2] * v2[2] };
// output:
cout << v3 << endl;
// done
return 0;
}
输出:
{ a, aa, aaa, b, bb, bbb, c, cc, ccc }
相关文章:
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 尝试通过多个向量访问变量时,向量下标超出范围
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 我有三个 getline,但是一旦编译,输入就太多了
- 比较 2 个向量并从第二个向量中删除在第一个 - c++ 中找不到的元素
- 在C++中查找两个向量之间最相似的值
- C++在 2 个向量向量之间交换向量
- 交叉连接 2 个向量的元素以产生第三个
- 五个中三个最大参数的平均值,不使用排序算法或向量/数组
- 在C++中将文件读入三个不同的向量
- 将函数两个向量合并为第三个向量
- 计算三个或更多向量之间的向量
- 有没有办法像python:np.concatenate([x,y,z],axis=1)中那样沿着列连接三个二维向量C++
- 根据列中的第三个数字对向量进行排序
- 有效地检验三个二进制向量在有限域上是否线性无关
- 用c++实现程序,通过附加另外两个向量来输出第三个向量
- 将两个 std::vector<cv::P oint> 向量和安全公共点与第三个 std::vector<cv::P oint 进行比较>
- 排序三个向量,基于一个的排列,有重复项