Skip to content

Instantly share code, notes, and snippets.

@wnduan
Last active September 25, 2016 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wnduan/892c4b3abc5a33abcc2351ba1bd1f997 to your computer and use it in GitHub Desktop.
Save wnduan/892c4b3abc5a33abcc2351ba1bd1f997 to your computer and use it in GitHub Desktop.
A python problem from v2ex: https://www.v2ex.com/t/308543#;
# -*- coding: utf-8 -*-
t1 = [{'id':1, 'abc':'2'}, {'id':1, 'abc':'3'}, {'id':2, 'abc':'2'}]
t2 = []
ids = []
# Get all id values
for item in t1:
ids.append(item['id'])
ids = sorted(list(set(ids)))
# For each id get according 'abc' values and generate the result
for i in ids:
temp = []
for item in t1:
if item['id'] == i:
temp.append(item['abc'])
if len(temp) == 1:
temp = temp[0]
else:
temp.sort()
t2.append({'id':i, 'abc':temp})
print(t2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment