by merlincorey · 4y ago · 0 comments
I really wanted to see what it was gonna do for Python -> C++ converting some nested dictionary and list comprehensions:
def foobar(x, y):
return {
_: __
for _, __ in zip(
[_ for _ in x if _ in y],
[_ for _ in x if _ not in y]
)
}
{3: 1, 4: 2} == foobar([1, 2, 3, 4], [3, 4, 5, 6]) std::map<int, int> foobar(std::vector<int> x, std::vector<int> y) {
std::map<int, int> map;
for (int i = 0; i < x.size(); i++) {
if (std::find(y.begin(), y.end(), x[i]) != y.end()) {
map[x[i]] = x[i + 1];
}
}
return map;
} #include <algorithm>
#include <iostream>
#include <map>
#include <vector>
std::map<int, int> foobar(std::vector<int> x, std::vector<int> y) {
std::map<int, int> map;
for (int i = 0; i < x.size(); i++) {
if (std::find(y.begin(), y.end(), x[i]) != y.end()) {
map[x[i]] = x[i + 1];
}
}
return map;
}
int main(int argc, char** argv) {
std::vector<int> ex{ 1, 2, 3, 4 };
std::vector<int> wy{ 3, 4, 5, 6 };
std::map<int, int> result = foobar(ex, wy);
std::cout << "{";
for ( const auto &item : result) {
std::cout << item.first << ": " << item.second << ", ";
}
std::cout << "}" << std::endl;
}
It outputs: {3: 4, 4: 0, }
But it SHOULD be getting: {3: 1, 4: 2, }