Last active
September 25, 2016 22:45
-
-
Save wnduan/892c4b3abc5a33abcc2351ba1bd1f997 to your computer and use it in GitHub Desktop.
A python problem from v2ex: https://www.v2ex.com/t/308543#;
This file contains 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
# -*- 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