reorx
6/10/2014 - 3:48 PM

A list merge question

A list merge question

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import timeit
import datetime
import cProfile

now = datetime.datetime(2014, 6, 10, 23, 19, 20, 0)
ms1 = datetime.timedelta(microseconds=1)
s1 = datetime.timedelta(seconds=1)

sample = [
    {
        'created': now + s1,
        'a': 4,
        'b': 14
    },
    {
        'created': now,
        'a': 1,
        'b': 11
    },
    {
        'created': now + s1 + ms1,
        'a': 5,
        'b': 15
    },
    {
        'created': now + ms1 + ms1,
        'a': 3,
        'b': 13
    },
    {
        'created': now + ms1,
        'a': 2,
        'b': 12
    },
]


def solution1():
    d = {}
    for i in sample:
        # Create item
        item = i.copy()
        del item['created']

        formatted = i['created'].strftime('%Y/%m/%d %H:%M:%S')
        d.setdefault(formatted, []).append(item)

    output = [{'created': k, 'items': v} for k, v in d.iteritems()]
    return output


def solution2():
    output = []
    last_formatted = None
    for i in sorted(sample, key=lambda x: x['created']):
        # Create item
        item = i.copy()
        del item['created']

        formatted = i['created'].strftime('%Y/%m/%d %H:%M:%S')
        if last_formatted is None or last_formatted != formatted:
            last_formatted = formatted
            new_i = {
                'created': formatted,
                'items': [item]
            }
            output.append(new_i)
        else:
            new_i['items'].append(item)
    return output


if __name__ == '__main__':
    # Usability test
    print 'Solution 1:'
    print json.dumps(solution1(), indent=4)

    print 'Solution 2:'
    print json.dumps(solution2(), indent=4)

    # Benchmark
    print 'Solution 1 timeit:'
    print timeit.timeit('solution1()', setup='from __main__ import solution1', number=10000)

    print 'Solution 2 timeit:'
    print timeit.timeit('solution2()', setup='from __main__ import solution2', number=10000)

    print 'Solution 1 profiling:'
    cProfile.run('[solution1() for i in xrange(10000)]')

    print 'Solution 2 profiling:'
    cProfile.run('[solution2() for i in xrange(10000)]')