Я пытаюсь запустить скрипт, который сравнивает два массива, извлеченных из некоторого json. Скрипт работает, но не оптимизирован вообще. Я хотел бы немного помочь относительно условий if, я хотел бы знать, есть ли лучший способ сделать это (я уверен на 100%). Большое спасибо за вашу помощь!
def compare(id1, id2, nb):
if id2 >= id1:
return 1 - (id2 - id1) / nb
else:
return 1 - (id1 - id2) / nb
searchList = ['student', 'runner', 'fireman', 'chief', 'teacher', 'seller', 'fisher', 'policeman']
for e in searchList:
for id1, item1 in enumerate(data1[e]['items']):
for id2, item2 in enumerate(data2[e]['items']):
if e == 'runner':
if item1['details']['profile_link'] == item2['details']['profile_link']:
res += compare(id1, id2, nb)
elif e == 'policeman' or e == 'products':
if item1['title'] == item2['title']:
res += compare(id1, id2, nb)
elif e == 'fisher':
if item1['description'] == item2['description']:
res += compare(id1, id2, nb)
elif e == 'chief':
if item1['program']['one'] and item2['program']['one']:
if item1['program']['one']['title'] == item1['program']['one']['title']:
res += compare(id1, id2, nb)
elif item1['program']['two'] and item2['program']['two']:
if item1['program']['two']['title'] == item1['program']['two']['title']:
res += compare(id1, id2, nb)
else:
if item1['profile_link'] == item2['profile_link']:
res += compare(id1, id2, nb)
res = res * 1000
Спасибо большое!
Редактировать № 1:
Спасибо за вашу помощь,
Вот пример моей структуры JSON:
{
"student": {
"count": 1,
"items": [
{
"index": 0,
"profile_link": "xxx"
}
]
},
"runner": {
"count": 1,
"items": [
{
"index": 0,
"details": [
{
"profile_link": "xxx"
}
]
}
]
},
"policeman": {
"count": 1,
"items": [
{
"index": 0,
"title": "xxx"
}
]
},
"fisher": {
"count": 1,
"items": [
{
"index": 0,
"description": "xxx"
}
]
},
"chief": {
"count": 1,
"items": {
"program": {
"one": [
{
"index": 0,
"title": "xxx",
}
],
"two": [
{
"index": 0,
"title": "xxx",
}
]
}
}
},
"fireman": {
"count": 1,
"items": [
{
"index": 0,
"profile_link": "xxx"
}
]
},
"teacher": {
"count": 1,
"items": [
{
"index": 0,
"profile_link": "xxx"
}
]
},
"seller": {
"count": 1,
"items": [
{
"index": 0,
"profile_link": "xxx"
}
]
}
}