If necessary, this iteration and dict accessing could be done more quickly in Cython. Otherwise, looks fine.