代码之家  ›  专栏  ›  技术社区  ›  Casebash

旧版本python的ordereddict

  •  57
  • Casebash  · 技术社区  · 16 年前

    有序字典是非常有用的结构,但不幸的是,这些字典是最近才使用的版本 3.1 2.7 . 如何在旧版本中使用有序字典?

    7 回复  |  直到 8 年前
        1
  •  59
  •   Arthur Ulfeldt    14 年前

    我用pip在python 2.6上安装了ordereddict

    pip install ordereddict
    
        2
  •  22
  •   Casebash    16 年前

    根据 documentation ,对于Python 2.4或更高版本 this code 应该使用。还有一些 code from Raymond Hettinger ,贡献者之一 PEP . 这里的代码声称在2.6和3.0下有效,并且是为提案而制定的。

        3
  •  11
  •   Mike T    10 年前

    要为不同版本的python导入ordereddict类,请考虑以下代码段:

    try:
        from collections import OrderedDict
    except ImportError:
        from ordereddict import OrderedDict
    
    # Now use it from any version of Python
    mydict = OrderedDict()
    

    需要安装比python 2.6旧的版本 ordereddict (使用PIP或其他方法),但较新版本将从内置集合模块导入。

        4
  •  1
  •   user6332699    9 年前

    此外,如果您的情况允许,您可以对其进行编程:

    def doSomething(strInput): return [ord(x) for x in strInput]
    
    things = ['first', 'second', 'third', 'fourth']
    
    oDict = {}
    orderedKeys = []
    for thing in things:
        oDict[thing] = doSomething(thing)
        orderedKeys.append(thing)
    
    for key in oDict.keys():
        print key, ": ", oDict[key]
    
    print
    
    for key in orderedKeys:
        print key, ": ", oDict[key]
    

    秒:[115、101、99、111、110、100]
    第四个:【102、111、117、114、116、104】
    第三个:【116、104、105、114、100】
    第一个:【102、105、114、115、116】

    第一个:【102、105、114、115、116】
    秒:[115、101、99、111、110、100]
    第三个:【116、104、105、114、100】
    第四个:【102、111、117、114、116、104】

    我想,您也可以将已排序的键嵌入字典中,正如odict['keylist']=ordered keys

        5
  •  1
  •   juggernaut    8 年前

    你也可以试试 future ,PY2-3兼容代码库:

    1. 安装 未来 通过PIP:

    pip install future

    1. 进口和使用订单信息:

    from future.moves.collections import OrderedDict

    source

        6
  •  0
  •   noragen    10 年前

    在python2.6给我的:

    $ pip install ordereddict
      Could not find a version that satisfies the requirement from (from versions:)
    No matching distribution found for from
    

    但是

    $ easy_install ordereddict
    install_dir /usr/local/lib/python2.6/dist-packages/
    Searching for ordereddict
    Reading http://pypi.python.org/simple/ordereddict/
    Best match: ordereddict 1.1
    Downloading https://pypi.python.org/packages/source/o/ordereddict/ordereddict-1.1.tar.gz#md5=a0ed854ee442051b249bfad0f638bbec
    Processing ordereddict-1.1.tar.gz
    Running ordereddict-1.1/setup.py -q bdist_egg --dist-dir /tmp/easy_install-lYgPE3/ordereddict-1.1/egg-dist-tmp-GF2v6g
    zip_safe flag not set; analyzing archive contents...
    Adding ordereddict 1.1 to easy-install.pth file
    
    Installed /usr/local/lib/python2.6/dist-packages/ordereddict-1.1-py2.6.egg
    Processing dependencies for ordereddict
    Finished processing dependencies for ordereddict
    

    做。

        7
  •  -1
  •   vitiral    10 年前

    对于那些由于某种原因不能依赖于用户拥有PIP的人,这里有一个 真可怕 ordereddict的实现(它是不可变的,具有大多数特性,但没有性能提升)。

    class OrderedDict(tuple):
        '''A really terrible implementation of OrderedDict (for python < 2.7)'''
        def __new__(cls, constructor, *args):
            items = tuple(constructor)
            values = tuple(n[1] for n in items)
            out = tuple.__new__(cls, (n[0] for n in items))
            out.keys = lambda: out
            out.items = lambda: items
            out.values = lambda: values
            return out
    
        def __getitem__(self, key):
            try:
                return next(v for (k, v) in self.items() if k == key)
            except:
                raise KeyError(key)