有没有一种方便的方法来获取C++ 11 foreach循环中当前容器条目的索引,就像enumerate在python中一样:
for idx, obj in enumerate(container):
pass
Run Code Online (Sandbox Code Playgroud)
我可以想象一个迭代器也可以返回索引或类似的东西.
当然我可以有一个计数器,但通常迭代器不保证它们在容器上迭代的顺序.
这个问题与以下问题有关,但在那里没有回答:
我有一个python模块具有以下树结构:
mcts
|- setup.py
|- mcts
|- __init__.py
|- uct.py
|- toy_world_state.py
|- test
|- test_uct.py
|- test_toy_world_state.py
Run Code Online (Sandbox Code Playgroud)
我在某个目录中创建了virtualenv
$ mkdir virtual
$ virtualenv --system-site-packages virtual
$ source virtual/bin/activate
Run Code Online (Sandbox Code Playgroud)
然后我安装我的包:
$ cd /path/to/mcts
$ pip install -e .
Run Code Online (Sandbox Code Playgroud)
现在我尝试运行测试:
$ py.test mcts
================================================== test session starts ==================================================
platform linux -- Python 3.4.2 -- py-1.4.26 -- pytest-2.6.4
collected 0 items / 2 errors
======================================================== ERRORS =========================================================
__________________________________ ERROR collecting mcts/test/test_toy_world_state.py ___________________________________
mcts/test/test_toy_world_state.py:4: in <module>
from mcts.toy_world_state import * …Run Code Online (Sandbox Code Playgroud) 我发现C++解析运算符重载的奇怪行为,我无法解释自己.指向描述它的某个资源的指针就像答案一样好.
我有2个翻译单位.在一个(称为util.cpp/h)中,我声明并定义了两个运算符(我省略了readabilty的实际实现,无论如何都会发生problam):
// util.h
#ifndef GUARD_UTIL
#define GUARD_UTIL
#include <iostream>
std::istream& operator>>(std::istream& is, const char* str);
std::istream& operator>>(std::istream& is, char* str);
#endif
Run Code Online (Sandbox Code Playgroud)
和:
//util.cpp
#include "util.h"
#include <iostream>
std::istream& operator>>(std::istream& is, const char* str) {
return is;
}
std::istream& operator>>(std::istream& is, char* str) {
return is;
}
Run Code Online (Sandbox Code Playgroud)
如果在全局命名空间中,这些运算符是因为它们在std类型和内置类型上运行,并且应该可以在任何地方使用.它们只能从全局命名空间(例如来自main())或明确地告诉编译器它们在全局命名空间中工作(参见代码示例).
在另一个翻译单元(称为test.cpp/h)中,我在命名空间中使用这些运算符.这有效,直到我将类似的运算符放入此命名空间.一旦添加此运算符,编译器(例如gcc或clang)就不能再找到可行的运算符>>.
// test.h
#ifndef GUARD_TEST
#define GUARD_TEST
#include <iostream>
namespace Namespace {
class SomeClass {
public:
void test(std::istream& is);
};
// without the following line everything compiles just fine
std::istream& operator>>(std::istream& is, …Run Code Online (Sandbox Code Playgroud) 假设df.bun(df 是 Pandas 数据帧)是一个多索引(日期和名称),变量是用字符串写入的类别值,
date name values
20170331 A122630 stock-a
A123320 stock-a
A152500 stock-b
A167860 bond
A196030 stock-a
A196220 stock-a
A204420 stock-a
A204450 curncy-US
A204480 raw-material
A219900 stock-a
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它代表同一日期的总计数及其百分比,以便用每个日期制作如下表格,
date variable counts Percentage
20170331 stock 7 70%
bond 1 10%
raw-material 1 10%
curncy 1 10%
Run Code Online (Sandbox Code Playgroud)
我已经解决print(df.groupby('bun').count())了这个问题,但它缺乏..
cf) 在获取 df.bun 之前,我使用以下代码将嵌套字典导入到 Pandas 数据框。
import numpy as np
import pandas as pd
result = pd.DataFrame()
origDict = np.load("Hannah Lee.npy")
for item in range(len(origDict)):
newdict = {(k1, k2):v2 …Run Code Online (Sandbox Code Playgroud) 我有一个简单的小装饰器,它将函数调用的结果缓存dict为函数属性.
from decorator import decorator
def _dynamic_programming(f, *args, **kwargs):
try:
f.cache[args]
except KeyError:
f.cache[args] = f(*args, **kwargs)
return f.cache[args]
def dynamic_programming(f):
f.cache = {}
return decorator(_dynamic_programming, f)
Run Code Online (Sandbox Code Playgroud)
我现在想添加清空缓存的可能性.所以我改变了dynamic_programming()这样的功能:
def dynamic_programming(f):
f.cache = {}
def clear():
f.cache = {}
f.clear = clear
return decorator(_dynamic_programming, f)
Run Code Online (Sandbox Code Playgroud)
现在让我们假设我使用这个小东西来实现Fibonacci数函数:
@dynamic_programming
def fib(n):
if n <= 1:
return 1
else:
return fib(n-1) + fib(n-2)
>>> fib(4)
5
>>> fib.cache
{(0,): 1, (1,): 1, (2,): 2, (3,): 3, (4,): 5}
Run Code Online (Sandbox Code Playgroud)
但是现在当我清除缓存时会发生一些奇怪的事情:
>>> …Run Code Online (Sandbox Code Playgroud) 我有一些基本的继承树:
class Base {
virtual double func() = 0;
// functionality is not important for the problem
// but it's good to know that Base has some virtual functions
};
class DerivedA : public Base {
virtual double func() {}; // implementation A
};
class DerivedB : public Base {
virtual double func() {}; // implementation B
};
Run Code Online (Sandbox Code Playgroud)
我有一个容器,指向一个DerivedA或多个DerivedB实例的指针.
void f1(std::vector<Base*> a) () { /* some code */ }
int main(in, char**) {
std::vector<Base*> base_container;
f1(base_container); …Run Code Online (Sandbox Code Playgroud) c++ ×3
python ×3
c++11 ×1
containers ×1
foreach ×1
inheritance ×1
operators ×1
pandas ×1
percentage ×1
pytest ×1