Код: Выделить всё
[
{
"type": "BEGIN",
"id": "XYZ123"
},
{
"type": "END",
"id": "XYZ123",
},
{
"type": "BEGIN",
"id": "XYZ456"
},
{
"type": "END",
"id": "XYZ987"
},
{
"type": "END",
"id": "XYZ456"
},
{
"type": "BEGIN",
"id": "XYZ789"
},
{
"type": "",
"id": "XYZ345"
}
]
valid_records.lst:
Код: Выделить всё
"id": "XYZ123"
"id": "XYZ456"
Код: Выделить всё
"id": "XYZ987"
"id": "XYZ789"
"id": "XYZ345"
Это фрагмент кода, который я пробовал до сих пор: `
Код: Выделить всё
valid_records=[]
error_records=[]
for rec in records:
if rec.get("type") == "BEGIN":
for rec1 in records:
if rec['id'] == rec1['id'] and rec1.get("type") == "END":
valid_records.append(rec1)
print("valid id : ", rec1['id'])
elif rec['id'] == rec1['id'] and rec1.get("type") != "END":
print("ignore, duplicate")
else:
error_records.append(rec1)
print("error id : ", rec1['id'])
Подробнее здесь: https://stackoverflow.com/questions/787 ... or-records