如何在二维数组中添加列值?例如
[[11.9, 12.2, 12.9],
[15.3, 15.1, 15.1],
[16.3, 16.5, 16.5],
[17.7, 17.5, 18.1]]
我希望它导致:
[61.2,61.3,62.6]
到目前为止我已尝试过这个:
Btotals.append(sum(float(x)) for x in zip(totals))
然而它给了我这个:
[<generator object <genexpr> at 0x02A42878>]
答案 0 :(得分:1)
你需要首先解压缩参数。
a = [[11.9, 12.2, 12.9],
[15.3, 15.1, 15.1],
[16.3, 16.5, 16.5],
[17.7, 17.5, 18.1]]
result = [sum(x) for x in zip(*a)]
>>> [61.2, 61.3, 62.6]
答案 1 :(得分:0)
如果#include <iostream>
int x = 0;
class MyClass {
private:
int x;
public:
MyClass() : x(0) {}
void test(int x) {
this->report(x);
std::cout << "Doing some magic...\n";
this->x += x;
this->report(x);
}
void report(int x) {
std::cout << "this->x = " << this->x << '\n';
std::cout << "x = " << x << '\n';
}
};
int main() {
MyClass c;
c.report();
x += 123;
c.test(x);
x += 456;
c.test(x);
c.report();
}
是包含2d数组的变量,请尝试
array