您好,登錄后才能下訂單哦!
背景:有一個list,里面的每一個元素都是dict,根據某一個key進行去重,在這里,key代表question
#!/usr/bin/env python # -*- coding: utf-8 -*- # created by fhqplzj on 2017/12/07 上午11:38 from itertools import groupby from operator import itemgetter import pandas as pd def distinct(items): questions = map(itemgetter('question'), items) df = pd.DataFrame({ 'items': items, 'questions': questions }) return df.drop_duplicates(['questions'])['items'].tolist() def distinct2(items): exist_questions = set() result = [] for item in items: question = item['question'] if question not in exist_questions: exist_questions.add(question) result.append(item) return result def distinct3(items): key = itemgetter('question') items = sorted(items, key=key) return [next(v) for _, v in groupby(items, key=key)] def distinct4(items): from itertools import compress mask = (~pd.Series(map(itemgetter('question'), items)).duplicated()).tolist() return list(compress(items, mask)) if __name__ == '__main__': data = [ {'question': 'a', 'ans': 'b'}, {'question': 'b', 'ans': 'd'}, {'question': 'a', 'ans': 'p'}, {'question': 'b', 'ans': 'e'} ] print distinct4(data)
以上這篇python去重,一個由dict組成的list的去重示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。