我目前正在使用SciPy.integrate.ode在 Python 中实现一个复杂的微生物食物网。我需要能够轻松地将物种和反应添加到系统中,所以我必须编写一些非常通用的代码。我的方案看起来像这样:
class Reaction(object):
def __init__(self):
#stuff common to all reactions
def __getReactionRate(self, **kwargs):
raise NotImplementedError
... Reaction subclasses that
... implement specific types of reactions
class Species(object):
def __init__(self, reactionsDict):
self.reactionsDict = reactionsDict
#reactionsDict looks like {'ReactionName':reactionObject, ...}
#stuff common to all species
def sumOverAllReactionsForThisSpecies(self, **kwargs):
#loop over all the reactions and return the
#cumulative change in the concentrations of all solutes
...Species subclasses where for each species
... are defined and passed to the superclass constructor
class FermentationChamber(object):
def __init__(self, speciesList, timeToSolve, *args):
#do initialization
def step(self):
#loop over each species, which in turn loops
#over each reaction inside it and return a
#cumulative dictionary of total change for each
#solute in the whole system
if __name__==__main__:
f = FermentationChamber(...)
o = ode(...) #initialize ode solver
while o.successful() and o.t<timeToSolve:
o.integrate()
#process o.t and o.y (o.t contains the time points
#and o.y contains the solution matrix)
所以,问题是,当我在Species.sumOverAllReactionsForThisSpecies()
and中迭代字典时FermentationChamber.step()
,如果在第一次和最后一次迭代之间没有从字典中添加或删除任何元素,字典的迭代顺序是否保证相同?也就是说,我可以假设字典中每次迭代创建的 numpy 数组的顺序不会改变吗?例如,如果字典的格式为 {'Glucose':10, 'Fructose':12},如果从该字典创建的 Array 将始终具有相同的顺序(无论该顺序是什么,只要这是确定性的)。
对不起,我只是想让你知道我来自哪里。