圆月山庄资源网 Design By www.vgjia.com

背景:有一个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的去重示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,dict,list,去重

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com