【问题标题】:Remove redundant items from a list of dicts从字典列表中删除冗余项
【发布时间】:2023-01-31 17:44:26
【问题描述】:

我有一个基于用户从 GUI 选择的字典列表(Plotly 返回)。当用户单击一个数据点(或一组数据点)时,数据点将添加到列表中。

但是,如果用户单击相同的数据点(或选择一组数据点,其中包括已选择的数据点)然后冗余字典出现在冗余数据点的列表中.

IE。

[
  {  
    "clicked": true,
    "selected": true,
    "hovered": false,
    "x": 0,
    "y": 71100.0988957607,
    "selected_xcol": "injection_id",
    "xvalue": "e54112f9-4497-4a7e-91cd-e26842a4092f",
    "selected_ycol": "peak_area",
    "yvalue": 71100.0988957607,
    "injection_id": "e54112f9-4497-4a7e-91cd-e26842a4092f"
  },
  {
    "clicked": true,
    "selected": true,
    "hovered": false,
    "x": 0,
    "y": 75283.2386064552,
    "selected_xcol": "injection_id",
    "xvalue": "e54112f9-4497-4a7e-91cd-e26842a4092f",
    "selected_ycol": "peak_area",
    "yvalue": 75283.2386064552,
    "injection_id": "e54112f9-4497-4a7e-91cd-e26842a4092f"
  },
  {  # Redundant, same as first item
    "clicked": true,
    "selected": true,
    "hovered": false,
    "x": 0,
    "y": 71100.0988957607,
    "selected_xcol": "injection_id",
    "xvalue": "e54112f9-4497-4a7e-91cd-e26842a4092f",
    "selected_ycol": "peak_area",
    "yvalue": 71100.0988957607,
    "injection_id": "e54112f9-4497-4a7e-91cd-e26842a4092f"
  }
]

因为用户可以在一个 GUI 笔画中选择一个或多个数据点,而代码不知道是哪个,所以我只是将返回的列表添加到累积列表中,就像这样......

LOCAL["selected_data"] += selectable_data_chart(LOCAL["df"], 
                                               key = "st_react_plotly_control_main_chart",
                                               custom_data_columns = custom_data_columns, 
                                               hovertemplate = hovertemplate, 
                                               svgfilename = svgfilename)

我试过用...过滤掉多余的项目

LOCAL["selected_data"] = list(set(LOCAL["selected_data"]))

...但它会引发错误...

TypeError: unhashable type: 'dict'

我也试过...

result = []
LOCAL["selected_data"] = [result.append(d) for d in LOCAL["selected_data"] if d not in result]  

...但无论如何它都会返回 null 。

[
  null,
  null
] 

【问题讨论】:

  • 看起来您要合并的词典的某些键具有不同的值。你能定义什么时候两个字典应该被认为是重复的吗?
  • 对不起。打字错误。 ..

标签: python-3.x list dictionary


【解决方案1】:

您不能将可变值添加到集合(或将其用作字典键)...如果在将项目添加到集合后更改值以使其与另一个集合成员相同怎么办?这将使 set 数据类型提供的保证无效。

一种可能的解决方案是将您的字典转换为结构化类型。例如,使用 dataclasses 模块,我们可以这样写(假设您的示例数据包含在文件 data.json 中):

import json
import dataclasses


@dataclasses.dataclass(frozen=True)
class Event:
    clicked: bool
    selected: bool
    hovered: bool
    x: float
    y: float
    selected_xcol: str
    xvalue: float
    selected_ycol: str
    yvalue: float
    injection_id: str


with open("data.json") as fd:
    data = json.load(fd)

events = set(Event(**item) for item in data)

正如@lemon 在评论中指出的那样,这实际上不适用于您问题中的示例数据,因为列表中的第三项是不是与第一项相同(在第一项中,x=0,但在第三项中,x="e54112f9-4497-4a7e-91cd-e26842a4092f")。如果这只是输入问题时的错字,这里的解决方案就可以正常工作。


结构化程度较低的解决方案是使用 items() 方法将每个字典转换为元组列表,将其转换为 tuple,然后将它们添加到您的“唯一”集合中:

import json

with open("data.json") as fd:
    data = json.load(fd)

events = set(tuple(item.items()) for item in data)

在这种情况下,events 是一组元组;您可以将其转换回像这样的字典列表:

dict_events = [dict(item) for item in events]

【讨论】:

    猜你喜欢
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2012-02-21
    • 2020-07-14
    • 2012-02-16
    相关资源
    最近更新 更多