在 Python 中获取不同的值
Getting Distinct value in Python
我有一个列表
[<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}>]
我想要这个的独特价值
预期结果是
[
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>
]
正在从数据库中提取此数据。请不要建议我在数据库上使用 DISTINCT 关键字。它已按另一列排序。使用 distinct 将删除排序结果。
能不能在python完成。或者我必须写一个 for 循环来做同样的事情吗?
MyCODE
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)))]
试试这个:
newList = list(set(oldList))
在上面,将您的列表变成一个集合(删除重复项),然后再返回一个列表。
有关详细信息:请参阅 this。看看有没有帮助。
经过反复试验,我又回到了 For 循环
def remove_dupes_keep_order(self,seq):
Lister=[]
for row in seq:
Dicter={"CaseID":row["CaseID"],"PatientProfile_ID":row["PatientProfile_ID"]}
Lister.append(Dicter)
seen = []
seen_add = seen.append
return [ x for x in Lister if not (x in seen or seen_add(x))]
代码段礼貌:@Richard
我有一个列表
[<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}>]
我想要这个的独特价值 预期结果是
[
<Storage {'CaseID': 45L, 'PatientProfile_ID': 3L}>,
<Storage {'CaseID': 46L, 'PatientProfile_ID': 2L}>
]
正在从数据库中提取此数据。请不要建议我在数据库上使用 DISTINCT 关键字。它已按另一列排序。使用 distinct 将删除排序结果。
能不能在python完成。或者我必须写一个 for 循环来做同样的事情吗?
MyCODE
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)))]
试试这个:
newList = list(set(oldList))
在上面,将您的列表变成一个集合(删除重复项),然后再返回一个列表。
有关详细信息:请参阅 this。看看有没有帮助。
经过反复试验,我又回到了 For 循环
def remove_dupes_keep_order(self,seq):
Lister=[]
for row in seq:
Dicter={"CaseID":row["CaseID"],"PatientProfile_ID":row["PatientProfile_ID"]}
Lister.append(Dicter)
seen = []
seen_add = seen.append
return [ x for x in Lister if not (x in seen or seen_add(x))]
代码段礼貌:@Richard