1. List existing dictionary (dictionary is the list element format) to sort
"""要求:按倒序排序"""
group_list = [{'group_code': 22, 'group_name': 'dev'},
{'group_code': 5, 'group_name': 'design'},
{'group_code': 34, 'group_name': 'sale'},
{'group_code': 15, 'group_name': 'manager'}]
group_list = sorted(group_list, key=lambda x: x['group_code'], reverse=True)
print group_list
group_list.sort(key=lambda x: x['group_code'], reverse=True)
print group_list
import operator
group_list = sorted(group_list, key=operator.itemgetter('group_code'), reverse=True)
print group_list
2. The classification dictionary list structure (i.e. packet key, the key value associated with the each key)
"""按学校分组,将该校校友关联到对应学校"""
school_student_info_list = [{'school_code': 'WH', 'student_id': 1},
{'school_code': 'WH', 'student_id': 2},
{'school_code': 'WH', 'student_id': 3},
{'school_code': 'WH', 'student_id': 4},
{'school_code': 'HU', 'student_id': 11},
{'school_code': 'HU', 'student_id': 12},
{'school_code': 'HU', 'student_id': 13},
{'school_code': 'HU', 'student_id': 14},
{'school_code': 'HTU', 'student_id': 21},
{'school_code': 'HTU', 'student_id': 22},
{'school_code': 'HTU', 'student_id': 23},
{'school_code': 'WTU', 'student_id': 31},
{'school_code': 'WTU', 'student_id': 32},
]
import collections
school_student_dict = collections.defaultdict(list)
for group in school_student_info_list:
school_student_dict[group['school_code']].append(group['student_id'])
print school_student_dict