Summarize a list of dictionaries based on common key values

后端 未结 3 948
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 01:15

I have a list of dictionaries like so:

dictlist = [{\'day\': 0, \'start\': \'8:00am\', \'end\': \'5:00pm\'},
            {\'day\': 1, \'start\': \'10:00am\', \'e         


        
相关标签:
3条回答
  • 2021-01-28 01:22

    With itertools.groupby:

    In [1]: %paste
    dictlist = [{'day': 0, 'start': '8:00am',  'end': '5:00pm'},
                {'day': 1, 'start': '10:00am', 'end': '7:00pm'},
                {'day': 2, 'start': '8:00am',  'end': '5:00pm'},
                {'day': 3, 'start': '10:00am', 'end': '7:00pm'},
                {'day': 4, 'start': '8:00am',  'end': '5:00pm'},
                {'day': 5, 'start': '11:00am', 'end': '1:00pm'}]
    
    ## -- End pasted text --
    
    In [2]: from itertools import groupby
    
    In [3]: tuplist = [(d['day'], (d['start'], d['end'])) for d in dictlist]
    
    In [4]: key = lambda x: x[1]
    
    In [5]: summarylist = [(sorted(e[0] for e in g),) + k
       ...:        for k, g in groupby(sorted(tuplist, key=key), key=key)]
    
    In [6]: summarylist
    Out[6]:
    [([1, 3], '10:00am', '7:00pm'),
     ([5], '11:00am', '1:00pm'),
     ([0, 2, 4], '8:00am', '5:00pm')]
    
    0 讨论(0)
  • 2021-01-28 01:26

    You can use itertools.groupby like this.

    source code:

    from itertools import groupby
    for k, grp in groupby(sorted(dictlist, key=lambda x:(x['end'], x['start'])), key=lambda x:(x['start'], x['end'])):
        print [i['day'] for i in grp], k
    

    output:

    [5] ('11:00am', '1:00pm')
    [0, 2, 4] ('8:00am', '5:00pm')
    [1, 3] ('10:00am', '7:00pm')
    

    But I think using defaultdict(@Akavall answer) is the right way in this particular case.

    0 讨论(0)
  • 2021-01-28 01:30

    If you don't need the exact format that you provide you could use defaultdict

    dictlist = [{'day': 0, 'start': '8:00am', 'end': '5:00pm'},
                {'day': 1, 'start': '10:00am', 'end': '7:00pm'},
                {'day': 2, 'start': '8:00am', 'end': '5:00pm'},
                {'day': 3, 'start': '10:00am', 'end': '7:00pm'},
                {'day': 4, 'start': '8:00am', 'end': '5:00pm'},
                {'day': 5, 'start': '11:00am', 'end': '1:00pm'}]
    
    from collections import defaultdict
    
    dd = defaultdict(list)
    
    for d in dictlist:
        dd[(d['start'],d['end'])].append(d['day'])
    

    Result:

    >>> dd
    defaultdict(<type 'list'>, {('11:00am', '1:00pm'): [5], ('10:00am', '7:00pm'): [1, 3], ('8:00am', '5:00pm'): [0, 2, 4]})
    

    And if format is important to you could do:

    >>> my_list = [(v, k[0], k[1]) for k,v in dd.iteritems()]
    >>> my_list
    [([5], '11:00am', '1:00pm'), ([1, 3], '10:00am', '7:00pm'), ([0, 2, 4], '8:00am', '5:00pm')]
    >>> # If you need the output sorted:  
    >>> sorted_my_list = sorted(my_list, key = lambda k : len(k[0]), reverse=True)
    >>> sorted_my_list
    [([0, 2, 4], '8:00am', '5:00pm'), ([1, 3], '10:00am', '7:00pm'), ([5], '11:00am', '1:00pm')]
    
    0 讨论(0)
提交回复
热议问题