交叉连接 2 个向量的元素以产生第三个

Cross Concatenate Elements of 2 Vectors to Produce a Third

本文关键字:三个 向量 元素 交叉连接      更新时间:2023-10-16

我有 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 }