【发布时间】: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