gpt4 book ai didi

python - 在 Python 中获取不同的值

转载 作者:行者123 更新时间:2023-11-29 02:57:39 27 4
gpt4 key购买 nike

我有一个 list

[<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>, 
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>,
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>]

我想要这个的独特值(value)预期结果是

[
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>
]

此数据是从数据库中提取的。请不要建议我在数据库上使用 DISTINCT 关键字。它已按另一列排序。使用 distinct 将删除排序结果。

可以用python实现吗?或者我是否必须编写一个 for 循环来做同样的事情?

我的代码

entries=db.query("SELECT cases.ID as CaseID,PatientProfile_ID FROM \
`callog` ,consultation,cases WHERE callog.Consultation_ID=consultation.ID and consultation.Case_ID = cases.ID and \
Doctor_ID="+str(Id)+" ORDER BY callog.CallDate DESC")
rows=entries.list()
return rows

最佳答案

这应该能很好地处理事情。

def remove_dupes_keep_order(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if not (x in seen or seen_add(x))]

使用seen_add 加速操作。

另请参阅,this SO question .

由于问题似乎是您的项目属于 Storage 类型,请尝试以下操作:

def remove_dupes_keep_order(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if not (json.dumps(x, sort_keys=True) in seen or seen_add(json.dumps(x, sort_keys=True)))]

关于python - 在 Python 中获取不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758921/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com