简体   繁体   中英

How to get corresponding values of a key in a JSON object in python where keys are in a different array

I have a JSON object which looks like this

{
'stats': {
    'stats_fields': {}
},
'responseHeader': {
    'stats': 0,
    'QTime': 312
},
'response': {
    'start': 0,
    'maxScore': 4.224531,
    'nmFond': 10,
    'docs': [{
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.14139599,
        'id': '635017352776'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.13736124,
        'id': '626248286912'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.14090206,
        'id': '667202821955'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.13128135,
        'id': '652653090703'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.15008019,
        'id': '673173069540'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.13817552,
        'id': '651864037354'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.1015507,
        'id': '366140762'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.049298372,
        'id': '621955821828'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.037345234,
        'id': '627999851992'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.03293659,
        'id': '647972845012'
    }]
}
}

I have another array having the Ids which looks like this

[626248286912, 366140762, 627999851992, 667202821955, 621955821828, 652653090703, 647972845012, 673173069540, 635017352776, 651864037354]

now i want to create an array having the value of key ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha)))) in the same order as the Ids in the second array . Is there a more efffcient way to do it in python than iterating and comparing the Ids in the json object

First your data is not really json because strings are protected by single quotes and json expects double quotes.

That can be workarounded by using ast.literal_eval for now.

So, I create d using ast.literal_eval so data is correctly converted to python native structure.

Then, I create a dictionary with key=id (as integer) and value as the value beared by the key different from id (using a nested generator comprehension that I consume using next to get the sole element)

After that, it's easy to query each value of the dictionary and compose the list in the required order.

Code:

import ast

js = """{
'stats': {
    'stats_fields': {}
},
'responseHeader': {
    'stats': 0,
    'QTime': 312
},
'response': {
    'start': 0,
    'maxScore': 4.224531,
    'nmFond': 10,
    'docs': [{
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.14139599,
        'id': '635017352776'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.13736124,
        'id': '626248286912'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.14090206,
        'id': '667202821955'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.13128135,
        'id': '652653090703'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.15008019,
        'id': '673173069540'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.13817552,
        'id': '651864037354'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.1015507,
        'id': '366140762'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.049298372,
        'id': '621955821828'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.037345234,
        'id': '627999851992'
    }, {
        'ml(div(map(abs(hpsaScore_alpha),0,100,abs(hpsaScore_alpha),100),100),ml(0.2,div(abs(hpsaScore_alpha),map(hpsaScore_alpha,0,0,1,hpsaScore_alpha))))': 0.03293659,
        'id': '647972845012'
    }]
}
}"""

idlist = [626248286912, 366140762, 627999851992, 667202821955, 621955821828, 652653090703, 647972845012, 673173069540, 635017352776, 651864037354]

d = ast.literal_eval(js)
by_id = {int(sd['id']):next(v for v in sd.values() if v!=sd['id']) for sd in d['response']['docs']}

result = [(a,by_id[a]) for a in idlist]
print(result)

result (if you just want the values and not the tuples do: [by_id[a] for a in idlist] ):

[(626248286912, 0.13736124), (366140762, 0.1015507), (627999851992, 0.037345234), (667202821955, 0.14090206), (621955821828, 0.049298372), (652653090703, 0.13128135), (647972845012, 0.03293659), (673173069540, 0.15008019), (635017352776, 0.14139599), (651864037354, 0.13817552)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM