by Pinus · 4y ago · 0 comments
Essentially, I'm agreeing with eska's comment, with the addition: "...but consider your data structures!"
groupby looks neat, but it requires sorted input, so it's very easy to convert an O(n) problem to an elegant but O(n log n) program.
EDIT: one could also do it with a one-liner in pandas, which means that as long as the input is less than ten million lines or so, the "import pandas as pd" statement is going to take longer than the actual program...
groupby_dict = collections.defaultdict(int)
for l in mylist:
groupby_dict[l['thing']] += l['count']
newlist = []
for thing, count in groupby_dict.items():
newlist.append(dict(thing=thing, count=count))
Knowing how do that does require the deep knowledge of data structures I'd expect every professional programmer to have, and doing a quick scan of the languages standard library (maybe an hours work) to see what it offers. After that you're set for solving not only this problem, but almost guaranteed to solve all problems you are likely to hit pretty optimally in Python. I have no idea where Stack Overflow fits into the picture.If you go a step further and do the Python standard tutorial found in the docs, you would discover how a finger weary experienced Python programmer might write those last three lines:
newlist = [dict(thing=thing, count=count) for thing, count in groupby_dict.items()]
Comprehensions are sweet Python syntactic sugar, but unlike mapping your existing encyclopaedic knowledge of data structures onto the languages standard library they aren't necessary for a casual user of the language. Indeed, some Python'istas will tell you resisting such delights make for clearer code.But trusting a quick Stack Overflow to tell you the optimal way to use a languages data structures - you must be kidding me.
groupby_dict = Counter()
for r in response:
groupby_dict.update({r['thing']: r['count']})
Counter is a subclass of dict, no further conversion needed.