gpt4 book ai didi

python - 我如何从字典中进行 AND 搜索,发送值列表?

转载 作者:太空宇宙 更新时间:2023-11-04 04:31:21 24 4
gpt4 key购买 nike

我有一个很大的 json 文件,其中包含以下格式的项目:

{"Disease":["Cholera due to Vibrio cholerae 01, biovar cholerae\r","Cholera due to Vibrio cholerae 01, biovar eltor\r","Cholera, unspecified\r","Typhoid fever, unspecified\r","Typhoid meningitis\r","Typhoid fever with heart involvement\r","Typhoid pneumonia\r","Typhoid arthritis\r","Typhoid osteomyelitis\r","Typhoid fever with other complications\r","Paratyphoid fever A\r","Paratyphoid fever B\r","Paratyphoid fever C\r","Paratyphoid fever, unspecified\r","Salmonella enteritis\r","Salmonella sepsis\r","Localized salmonella infection, unspecified\r"],"Code":["A000","A001","A009","A0100"]}

我的 django 程序将向需要执行 AND 搜索的脚本发送一个值列表(这意味着它将从字典返回一个值,如果所有搜索字符串都是 json 中 Disease 值的子字符串) .它将需要返回多个值作为 json 发送到 javascript 并呈现。

目前我可以使用以下代码进行 OR 搜索:

slist = ['myocardial', 'infarction']

def advanced_icd(request, slist):
import json
from myappointments.settings import PROJECT_ROOT
url = PROJECT_ROOT + '/appointments/static/clinic/db/icd10.json'
import pandas as pd
db = pd.read_json(path_or_buf=url)
codedb = db.to_dict()
result_Disease = []
result_Code = []
for (key, Disease), (key1, Code) in zip(codedb['Disease'].items(), codedb['Code'].items()):
for searchstr in slist:
searchstr = searchstr.lower()
if searchstr in Disease.lower():
print("Found a disease at key:%s Disease:%s Code:%s" %
(key, Disease, Code))
result_Disease.append(Disease)
result_Code.append(Code)
outp = json.dumps(
{'Disease': result_Disease, 'Code': result_Code})
print(outp)

advanced_icd("", slist)

Output:
Code:D735sease at key:2844 Disease:Infarction of spleen
Code:G43501ase at key:4777 Disease:Persistent migraine aura without cerebral infarction, not intractable, with status migrainosus

最佳答案

假设 db 是一个 pandas.DataFrame 实例,您可以使用DataFrame.loc 属性使用一系列 bool 值。例如:

df = pd.DataFrame({'var': ['a', 'b', 'c', 'd', 'e']})
print(df.loc[[True, True, False, False, True]])

这给出了这个输出:

  var
0 a
1 b
4 e

换句话说,对应于 True 的行被选中。

将此应用于您的问题,假设您有一个搜索词列表,如下所示:

slist = ['late', 'acute', 'deep']

你会定义一对辅助函数,它会告诉你,对于给定的disease,是否匹配这些搜索词:

cond_or = lambda disease: any(s.lower() in disease.lower() for s in slist)
cond_and = lambda disease: all(s.lower() in disease.lower() for s in slist)

然后剩下要做的就是将这些函数之一应用到 Disease列并使用结果来选择匹配的行:

>>> matching_diseases = db.loc[db.Disease.map(cond_and)]
>>> matching_diseases
Disease Code
6847 Acute embolism and thrombosis of other specifi... I82.493
7708 Acute embolism and thrombosis of unspecified d... I82.4Z3
21460 Acute embolism and thrombosis of unspecified d... I82.4Y3
33825 Acute embolism and thrombosis of unspecified d... I82.403
42904 Acute embolism and thrombosis of deep veins of... I82.623

因为 matching_diseases 也是一个 DataFrame,你可以简单地选择它获取与代码或疾病对应的序列的列:

result_Disease = matching_diseases['Disease']
result_Code = matching_diseases['Code']

关于python - 我如何从字典中进行 AND 搜索,发送值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52590042/

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