本文介绍了Python字典的顺序是否在迭代中得到保证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 SciPy.integrate.ode .我需要能够轻松地向系统中添加物种和反应的能力,因此我必须编写一些非常通用的东西.我的计划看起来像这样:

I'm currently implementing a complex microbial food-web in Python using SciPy.integrate.ode. I need the ability to easily add species and reactions to the system, so I have to code up something quite general. My scheme looks something like this:

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},则从此字典创建的数组总是的顺序相同(不管什么只要是确定性的,顺序就是这样.

So, the question is, when I iterate over the dictionaries in Species.sumOverAllReactionsForThisSpecies() and FermentationChamber.step(), is the iteration order of the dictionaries guaranteed to be the same if no elements are added or removed from the dictionaries between the first and the last iteration? That is, can I assume that the order of the numpy array created at each iteration from the dictionary will not vary? For example, if a dictionary has the format {'Glucose':10, 'Fructose':12}, if an Array created from this dictionary will always have the same order (it doesn't matter what that order is, as long as it's deterministic).

对不起,我只想告诉你我来自哪里.

Sorry for the mega-post, I just wanted to let you know where I'm coming from.

推荐答案

Python 3.1具有 collections.OrderedDict 类.这也非常有效:所有方法的Big-O运行时间与常规字典相同."

Python 3.1 has a collections.OrderedDict class that can be used for this purpose. It's very efficient, too: "Big-O running times for all methods are the same as for regular dictionaries."

OrderedDict本身的代码与Python 2.x兼容,尽管有些继承的方法(来自 _ abcoll 模块)确实使用仅限Python 3的功能.但是,可以轻松地将它们修改为2.x代码.

The code for OrderedDict itself is compatible with Python 2.x, though some inherited methods (from the _abcoll module) do use Python 3-only features. However, they can be modified to 2.x code with minimal effort.

这篇关于Python字典的顺序是否在迭代中得到保证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 11:45