list中的字典去重


list中的字典: 格式

list_dict = [{"a": "123", "b": "321"}, {"a": "1223", "b": "321"}, {"b": "321", "a": "123"}]

 

如上,list_dict中有三個字典,但是是重復的,這里需要去重,保留一個不重復的dict即可

def list_dict_duplicate_removal(list_dict):
    """list中dict重復的話,去重"""
    run_function = lambda x, y: x if y in x else x + [y]
    return reduce(run_function, [[], ] + list_dict)


all_test_data = list_dict_duplicate_removal(test_data)
print(f"去重,拿到所有商品信息,總數為:{len(list_dict)}")

輸出如下內容:

 

帶條件去重

def list_dict_duplicate_removal(list_dict):
    """list中dict重復的話,去重"""
    run_function = lambda x, y: x if dict(list(y.items())[:-1]) in [dict(list(a.items())[:-1]) for a in x] else x + [y]
    return reduce(run_function, [[], ] + list_dict)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM