Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- from itertools import groupby
- def lauritz(lst):
- key = lambda dct: dct['id']
- lst.sort(key=key)
- ret = []
- for key, group in groupby(lst, key=key):
- ret.append(max(group, key=lambda dct: dct['s']))
- return ret
- def ashwini(lst):
- dic={}
- for x in lst:
- idx=x['id']
- if dic.get('s',float('-inf')) < x ['s']:
- dic[idx]=x
- return dic.values()
- def martijn(lst):
- per_id = defaultdict(list)
- for entry in lst:
- per_id[entry['id']].append(entry)
- output = [max(v, key=lambda d: d['s']) for v in per_id.itervalues()]
- return output
- def thijs(lst):
- seen = set()
- output = [d for d in sorted(lst, key=lambda d: d['s'], reverse=True)
- if d['id'] not in seen and not seen.add(d['id'])]
- return output
- def jamylak(lst):
- res = {}
- for d in lst:
- id_ = d['id']
- res[id_] = max(res.get(id_, {}), d, key=lambda x: x.get('s', float('-inf')))
- return res.values()
- import random
- import time
- results = {}
- index = []
- for lst_len_pow in range(5, 19):
- lst_len = 2 ** lst_len_pow
- index.append(lst_len)
- lst = [{
- 'id': random.randint(1, 10),
- 's': random.uniform(0, 5)} for x in range(lst_len)]
- for func in (lauritz, ashwini, martijn, thijs, jamylak):
- start = time.time()
- func(lst)
- results.setdefault(func.__name__, []).append(time.time() - start)
- import pandas as pd
- import matplotlib.pyplot as plt
- df = pd.DataFrame(results, index=index)
- df.plot()
- plt.loglog()
- plt.xlabel("list lenght")
- plt.ylabel("execution time")
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement