【问题标题】:How to make ordered dictionary from list of lists?如何从列表列表中制作有序字典?
【发布时间】:2013-03-13 10:32:49
【问题描述】:

问题是: 有一个名称列表和一个列表列表,如何创建一个列表,其中每个项目都是一个有序字典,名称作为键,列表列表中的项目作为值?从下面的代码可能会更清楚:

from collections import OrderedDict

list_of_lists = [
                ['20010103', '0.9507', '0.9569', '0.9262', '0.9271'],
                ['20010104', '0.9271', '0.9515', '0.9269', '0.9507'],
                ['20010105', '0.9507', '0.9591', '0.9464', '0.9575'],
                ]

names = ['date', 'open', 'high', 'low', 'close']

我想得到:

ordered_dictionary = [
                     OrderedDict([('date', '20010103'), ('open', '0.9507'), ('high', '0.9569'), ('low', '0.9262'), ('close', '0.9271')]),
                     OrderedDict([('date', '20010104'), ('open', '0.9271'), ('high', '0.9515'), ('low', '0.9269'), ('close', '0.9507')]),
                     OrderedDict([('date', '20010105'), ('open', '0.9507'), ('high', '0.9591'), ('low', '0.9464'), ('close', '0.9575')]),
                     ]

【问题讨论】:

    标签: python list dictionary python-2.7 ordereddictionary


    【解决方案1】:

    使用zip() 组合名称和值。使用列表理解:

    from collections import OrderedDict
    
    ordered_dictionary = [OrderedDict(zip(names, subl)) for subl in list_of_lists]
    

    给出:

    >>> from pprint import pprint
    >>> pprint([OrderedDict(zip(names, subl)) for subl in list_of_lists])
    [OrderedDict([('date', '20010103'), ('open', '0.9507'), ('high', '0.9569'), ('low', '0.9262'), ('close', '0.9271')]),
     OrderedDict([('date', '20010104'), ('open', '0.9271'), ('high', '0.9515'), ('low', '0.9269'), ('close', '0.9507')]),
     OrderedDict([('date', '20010105'), ('open', '0.9507'), ('high', '0.9591'), ('low', '0.9464'), ('close', '0.9575')])]
    

    【讨论】:

    • 谢谢!清晰优雅的解决方案!
    【解决方案2】:

    我知道这个问题已经很老了,但我想我会建议一个 namedtuple 解决方案作为 OrderedDict 的替代方案,在这种情况下效果很好:

    from collections import namedtuple
    
    Bar = namedtuple('Bar', ['date', 'open', 'high', 'low', 'close'])
    
    bars = [Bar(date, o, h, l, c) for date, o, h, l, c in list_of_lists]
    
    >>> bars
    [Bar(date='20010103', open='0.9507', high='0.9569', low='0.9262', close='0.9271'),
     Bar(date='20010104', open='0.9271', high='0.9515', low='0.9269', close='0.9507'),
     Bar(date='20010105', open='0.9507', high='0.9591', low='0.9464', close='0.9575')]
    
    >>> bars[2].date
    '20010105'
    
    >>> bars[2].close
    '0.9575'
    

    更好的是,可以使用以日期为键的字典理解:

    Bar = namedtuple('Bar', ['open', 'high', 'low', 'close'])
    
    bars = {date: Bar(o, h, l, c) for date, o, h, l, c in list_of_lists}
    
    >>> bars
    {'20010103': Bar(open='0.9507', high='0.9569', low='0.9262', close='0.9271'),
     '20010104': Bar(open='0.9271', high='0.9515', low='0.9269', close='0.9507'),
     '20010105': Bar(open='0.9507', high='0.9591', low='0.9464', close='0.9575')}
    
    >>> bars['20010105']
    Bar(open='0.9507', high='0.9591', low='0.9464', close='0.9575')
    
    >>> bars['20010105'].close
    '0.9575'
    

    【讨论】:

      猜你喜欢
      • 2016-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 2014-07-08
      相关资源
      最近更新 更多