python去重,一个由dict组成的list的去重示例
背景:有一个list,里面的每一个元素都是dict,根据某一个key进行去重,在这里,key代表question
#!/usr/bin/envpython
#-*-coding:utf-8-*-
#createdbyfhqplzjon2017/12/07上午11:38
fromitertoolsimportgroupby
fromoperatorimportitemgetter
importpandasaspd
defdistinct(items):
questions=map(itemgetter('question'),items)
df=pd.DataFrame({
'items':items,
'questions':questions
})
returndf.drop_duplicates(['questions'])['items'].tolist()
defdistinct2(items):
exist_questions=set()
result=[]
foriteminitems:
question=item['question']
ifquestionnotinexist_questions:
exist_questions.add(question)
result.append(item)
returnresult
defdistinct3(items):
key=itemgetter('question')
items=sorted(items,key=key)
return[next(v)for_,vingroupby(items,key=key)]
defdistinct4(items):
fromitertoolsimportcompress
mask=(~pd.Series(map(itemgetter('question'),items)).duplicated()).tolist()
returnlist(compress(items,mask))
if__name__=='__main__':
data=[
{'question':'a','ans':'b'},
{'question':'b','ans':'d'},
{'question':'a','ans':'p'},
{'question':'b','ans':'e'}
]
printdistinct4(data)
以上这篇python去重,一个由dict组成的list的去重示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。