我目前正在使用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()
和FermentationChamber.step()
中的字典时,如果没有在第一个和第一个之间的字典中添加或删除任何元素,则字典的迭代顺序是否保证是相同的?最后一次迭代?也就是说,我是否可以假设在每次迭代中从字典创建的numpy数组的顺序都不会改变?例如,如果字典的格式为{'Glucose':10,'Fructose':12},则从该字典创建的数组将always具有相同的顺序(顺序无关紧要) ,只要是确定性的。
对不起,我只想告诉你我来自哪里。
Python 3.1具有可用于此目的的collections.OrderedDict类。这也非常有效:“所有方法的Big-O运行时间与常规字典相同。”
是,如果未修改,则保证相同的顺序。
提供的否
如果您希望订单保持一致,我会做一些事情来强制执行特定的订单。尽管您也许可以说服自己保证订单是正确的,而且您也许是对的,但对我来说这似乎很脆弱,对其他开发人员来说则是神秘的。
我也建议不要依赖词典顺序是非随机的事实。
取决于Python版本。