在列表中查找唯一列表包含许多字典python

时间:2019-06-07 05:12:17

标签: python python-3.x dictionary

我正在努力在列表中找到字典的唯一列表:

    internal string RenderList()
            {
                ConstructGrid();
                SetSort();

                var page = new Page() { EnableViewState = false };
                var form = new System.Web.UI.HtmlControls.HtmlForm();
                page.Controls.Add(form);
                if (Data != null && Data.PageInfo != null && Data.PageInfo.TotalRecords > 0)
        {
      form.Controls.Add(this);
       DataBind();
         }
        else
               var noResultsControl  =  (Page.LoadControl("~/Controls/Shared/NoResults.ascx") as NoResults)  
noResultsControl.PropertyOne =  "Sample"  ;
    form.Controls.Add(noResultsControl);
      return ControlLoader.RenderCustomControl(page);
            }

它应该返回:

d = [[{'x':1, 'y':2},{'x':1, 'y':2}], [{'x':3, 'y':4}, {'x':3, 'y':4}], [{'x':1, 'y':2}, {'x':1, 'y':2}]]

2 个答案:

答案 0 :(得分:3)

有点技巧,但是您可以尝试使用json.dumpsjson.loads

import json

d = set(json.dumps(i) for i in d)
d = [json.loads(i) for i in d]

输出:

[[{'x': 3, 'y': 4}, {'x': 3, 'y': 4}], [{'x': 1, 'y': 2}, {'x': 1, 'y': 2}]]

答案 1 :(得分:1)

一种使用下面的方式

[list(map(dict,k)) for k in set(tuple(tuple(j.items()) for j in i) for i in d)]
#[[{'x': 3, 'y': 4}, {'x': 3, 'y': 4}], [{'x': 1, 'y': 2}, {'x': 1, 'y': 2}]]