Created
November 29, 2012 18:16
-
-
Save meadhikari/4170888 to your computer and use it in GitHub Desktop.
Reduce the list and add to why on every deletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_input = [{'why': [u'The Matrix Revolutions'], 'title': u'The Matrix'}, | |
{'why': [u'The Matrix Revolutions'], 'title': u'Inception'}, | |
{'why': [u'The Matrix'], 'title': u'The Matrix Revolutions'}, | |
{'why': [u'The Matrix'], 'title': u'Inception'}, | |
{'why': [u'Saving Private Ryan'], 'title': u'The Hurt Locker'}, | |
{'why': [u'Black Hawk Down'], 'title': u'Saving Private Ryan'}, | |
{'why': [u'Black Hawk Down'], 'title': u'The Kingdom'}, | |
{'why': [u'Black Hawk Down'], 'title': u'The Hurt Locker'}, | |
{'why': [u'A Walk To Remember'], 'title': u'The Notebook'}, | |
{'why': [u'Cast Away'], 'title': u'Forrest Gump'}, | |
{'why': [u'Forrest Gump'], 'title': u'The Notebook'}, | |
{'why': [u'Body of Lies'], 'title': u'The Kingdom'}, | |
{'why': [u'Unthinkable'], 'title': u'Syriana'}, | |
{'why': [u'Lord of War'], 'title': u'Syriana'}, | |
{'why': [u'The Terminal'], 'title': u'Forrest Gump'}, | |
{'why': [u'Titanic'], 'title': u'The Notebook'}, | |
{'why': [u'Catch Me If You Can'], 'title': u'Forrest Gump'}, | |
{'why': [u'Syriana'], 'title': u'The Constant Gardener'}, | |
{'why': [u'Nothing But the Truth'], 'title': u'Syriana'}, | |
{'why': [u'Hotel Rwanda'], 'title': u'The Constant Gardener'}, | |
{'why': [u'Hotel Rwanda'], 'title': u'Syriana'}, | |
{'why': [u'Inception'], 'title': u'The Matrix'}, | |
{'why': [u'Inception'], 'title': u'The Matrix Revolutions'}, | |
{'why': [u'The Hurt Locker'], 'title': u'Saving Private Ryan'}, | |
{'why': [u'The Hurt Locker'], 'title': u'The Kingdom'}] | |
#remove all the duplicate 'title' and append each 'why' | |
my_output = [{'why': [u'Unthinkable',u'Lord of War',u'Hotel Rwanda'], 'title': u'Syriana'}] |
looks ugly, but does the job ;)
a = groupby(
sorted(
[(x['title'], x['why'][0]) for x in my_input]),
key=lambda x: x[0])
my_output = [{'title': x[0], 'why': [y[1] for y in x[1]]} for x in a]
oh, i forgot that you need to
from itertools import groupby
output = {}
for d in my_input:
whys = output.setdefault(d['title'], [])
whys.extend(d['why'])
And for the complete example which produces the correct output I'd go for:
output = {}
for d in my_input:
try:
unique = output[d['title']]
except KeyError:
unique = output[d['title']] = {'why': [], 'title': d['title']}
unique['why'].extend(d['why'])
output = output.values()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about the following piece of code?