python 字典去重(列表中包含字典)
def list_dict_duplicate_removal(electron_list_dict): """列表中字典去重""" run_function = lambda x, y: x if y in x else x + [y] return reduce(run_function, [[], ] + electron_list_dict) ``` 2. 列表中嵌套字典,按字典中得某个键去重 ``` def test(): list = [{'country_id': 15, 'country_zh_name': '美国', 'country_en_name': '美国'}, {'country_id': 15, 'country_zh_name': '美国', 'country_en_name': 'the United States'}, {'country_id': 20, 'country_zh_name': '德国', 'country_en_name': 'Germany'}] a = [] a.append(list[0]) for dict in list: # print len(l4) k = 0 for item in a: # print 'item' if dict['country_id'] != item['country_id']: k = k + 1 # continue else: break if k == len(a): a.append(dict) print(a) if __name__ == '__main__': test()