如何从python字典中选择元素并使用
如何从python字典中选择特定的元素、数组或对象?并将其转换为python字典数组(由“药物”分隔)如何从python字典中选择元素并使用,python,arrays,json,python-3.x,Python,Arrays,Json,Python 3.x,如何从python字典中选择特定的元素、数组或对象?并将其转换为python字典数组(由“药物”分隔) {'entities': [{'confidence': 0.9997971402351069, 'end': 7, 'entity': 'Drug', 'extractor': 'CRFEntityExtractor', 'start': 0, 'value': 'ACETATE'}, {'confidence': 0.9999706904447406,
{'entities': [{'confidence': 0.9997971402351069,
'end': 7,
'entity': 'Drug',
'extractor': 'CRFEntityExtractor',
'start': 0,
'value': 'ACETATE'},
{'confidence': 0.9999706904447406,
'end': 13,
'entity': 'strength',
'extractor': 'CRFEntityExtractor',
'start': 8,
'value': '25 MG'},
{'confidence': 0.9998453283892297,
'end': 15,
'entity': 'Dosage',
'extractor': 'CRFEntityExtractor',
'start': 14,
'value': '4'},
{'confidence': 0.9997755143972202,
'end': 32,
'entity': 'Form',
'extractor': 'CRFEntityExtractor',
'start': 16,
'value': 'SOLUTE CONCENTRE'},
{'confidence': 0.9977145988367829,
'end': 57,
'entity': 'Duration',
'extractor': 'CRFEntityExtractor',
'start': 50,
'value': '7 jours'},
{'confidence': 0.9372295171779627,
'end': 64,
'entity': 'Drug',
'extractor': 'CRFEntityExtractor',
'start': 58,
'value': 'ACIDAC'},
{'confidence': 0.9982578804309925,
'end': 71,
'entity': 'strength',
'extractor': 'CRFEntityExtractor',
'start': 65,
'value': '300 MG'},
{'confidence': 0.9999528533143575,
'end': 73,
'entity': 'Dosage',
'extractor': 'CRFEntityExtractor',
'start': 72,
'value': '5'},
{'confidence': 0.9999296591678211,
'end': 89,
'entity': 'Form',
'extractor': 'CRFEntityExtractor',
'start': 74,
'value': 'COMPRIME ENROBE'},
{'confidence': 0.9992446956485572,
'end': 114,
'entity': 'Duration',
'extractor': 'CRFEntityExtractor',
'start': 107,
'value': '1 jours'},
{'confidence': 0.9296845434758102,
'end': 136,
'entity': 'Drug',
'extractor': 'CRFEntityExtractor',
'start': 115,
'value': 'ACIDE ALENDRONIQUE GT'},
{'confidence': 0.9997948784340954,
'end': 142,
'entity': 'strength',
'extractor': 'CRFEntityExtractor',
'start': 137,
'value': '70 MG'},
{'confidence': 0.9999681362182076,
'end': 144,
'entity': 'Dosage',
'extractor': 'CRFEntityExtractor',
'start': 143,
'value': '7'},
{'confidence': 0.9999797650629707,
'end': 163,
'entity': 'Form',
'extractor': 'CRFEntityExtractor',
'start': 145,
'value': 'COMPRIME PELLICULE'},
{'confidence': 0.9999587657131175,
'end': 188,
'entity': 'Duration',
'extractor': 'CRFEntityExtractor',
'start': 181,
'value': '8 jours'}]}
对此
[{'confidence': 0.9997971402351069,
'end': 0,
'entity': 'Drug',
'extractor': 'CRFEntityExtractor',
'start': 7,
'value': 'ACETATE'
'relations': [{'confidence': 0.9997971402351069,
'end': 13,
'entity': 'strength',
'extractor': 'CRFEntityExtractor',
'start': 8,
'value': '25 MG'},
{'confidence': 0.9998453283892297,
'end': 15,
'entity': 'Dosage',
'extractor': 'CRFEntityExtractor',
'start': 14,
'value': '4'},
{'confidence': 0.9997755143972202,
'end': 32,
'entity': 'Form',
'extractor': 'CRFEntityExtractor',
'start': 16,
'value': 'SOLUTE CONCENTRE'},
{'confidence': 0.9977145988367829,
'end': 57,
'entity': 'Duration',
'extractor': 'CRFEntityExtractor',
'start': 50,
'value': '7 jours'}]
},
{'confidence': 0.9372295171779627,
'end': 58,
'entity': 'Drug',
'extractor': 'CRFEntityExtractor',
'start': 54,
'value': 'ACIDAC'
'relations': [{'confidence': 0.9982578804309925,
'end': 71,
'entity': 'strength',
'extractor': 'CRFEntityExtractor',
'start': 65,
'value': '300 MG'},
{'confidence': 0.9999528533143575,
'end': 73,
'entity': 'Dosage',
'extractor': 'CRFEntityExtractor',
'start': 73,
'value': '5'},
{'confidence': 0.9999296591678211,
'end': 89,
'entity': 'Form',
'extractor': 'CRFEntityExtractor',
'start': 74,
'value': 'COMPRIME ENROBE'},
{'confidence': 0.9992446956485572,
'end': 114,
'entity': 'Duration',
'extractor': 'CRFEntityExtractor',
'start': 107,
'value': '1 jours'}],
},
{'confidence': 0.9296845434758102,
'end': 136,
'entity': 'Drug',
'extractor': 'CRFEntityExtractor',
'start': 115,
'value': 'ACIDE ALENDRONIQUE GT'
'relations': [{'confidence': 0.9997948784340954,
'end': 137,
'entity': 'strength',
'extractor': 'CRFEntityExtractor',
'start': 142,
'value': '70 MG'},
{'confidence': 0.9999681362182076,
'end': 143,
'entity': 'Dosage',
'extractor': 'CRFEntityExtractor',
'start': 144,
'value': '7'},
{'confidence': 0.9999797650629707,
'end': 145,
'entity': 'Form',
'extractor': 'CRFEntityExtractor',
'start': 163,
'value': 'COMPRIME PELLICULE'},
{'confidence': 0.9999587657131175,
'end': 188,
'entity': 'Duration',
'extractor': 'CRFEntityExtractor',
'start': 188,
'value': '8 jours'}],
}
]
我找到了一种将python字典转换为数组的方法,但它只有一种“药物”
数据={'entities':[{'confidence':0.9995591084943068,'end':21,'entity':'druge','extractor':'CRFEntityExtractor','start':0,'value':'ACIDE ALENDRONIQUE GT'},{'confidence':0.999265646779927,'end':27,'entity':'strength','extractor':'CRFEntityExtractor','start':22,'value':'70 MG'},{'confidence':0.9999681660486393,'end':29,'entity':剂量,'extractor':'CRFEntityExtractor','start':28,'value':'7'},{'confidence':0.99797654756072,'end':48,'entity':'Form','extractor':'CRFEntityExtractor','start':30,'value':'COMPRIME PELLICULE'},{'confidence':0.9999587657131175,'end':73,'entity':'Duration','extractor':'CRFEntityExtractor','start':66,'value':'8 jours'}]}
from enum import Enum
class MEDICAL_ENTITY(str, Enum):
DRUG = 'Drug'
STRENGTH = 'Strength'
DURATION = 'Duration'
ROUTE = 'Route'
FORM = 'Form'
ADE = 'ADE'
DOSAGE = 'Dosage'
REASON = 'Reason'
FREAQUENCY = 'Frequency'
无论何时启动新实体,都需要将上一个实体附加到列表中 而非
药物的实体
需要追加到关系
列表中
def mapRasaEntityToMedicalEntity(data):
result = []
entity = None
for item in data['entities']:
if item['entity'] == MEDICAL_ENTITY.DRUG:
if entity:
result.append(entity) # Push previous entity onto list
entity = item
entity['relations'] = []
else:
entity['relations'].append(item)
result.append(entity) # Push the last entity
return result
代码:
输出:
{‘姓名’:‘汤姆·克鲁斯’,‘年龄’:56,‘出生于’:‘纽约州锡拉丘兹’,‘出生日期’:‘1962年7月3日’,‘照片’:“”
{'name':'Robert Downey Jr.,'age':53,'出生于':'纽约市','出生日期':'1965年4月4日','照片':''''今天早些时候你没有问过类似的问题吗?正如我在前面的问题中告诉你的,这不是JSON,JSON使用双引号,而不是单引号。我真的不知道如何称呼它,这就是为什么我称它为JsonIt是一本Python字典。好的,谢谢你
def mapRasaEntityToMedicalEntity(data):
result = []
entity = None
for item in data['entities']:
if item['entity'] == MEDICAL_ENTITY.DRUG:
if entity:
result.append(entity) # Push previous entity onto list
entity = item
entity['relations'] = []
else:
entity['relations'].append(item)
result.append(entity) # Push the last entity
return result
import json
my_json ="""{
"Actors": [
{
"name": "Tom Cruise",
"age": 56,
"Born At": "Syracuse, NY",
"Birthdate": "July 3, 1962",
"photo": "https://jsonformatter.org/img/tom-cruise.jpg"
},
{
"name": "Robert Downey Jr.",
"age": 53,
"Born At": "New York City, NY",
"Birthdate": "April 4, 1965",
"photo": "https://jsonformatter.org/img/Robert-Downey-Jr.jpg"
}
]
}"""
your_data = json.loads(my_json)
for i in your_data["Actors"]:
print(i)