There was a task to make such a thing on python: Through the py-zabbix I refer to the Zabbix API.

In response, I get such an array of data -

 [ { "itemid": "30171", "clock": "1535500848", "value": "21.6000", "ns": "117344023" }, { "itemid": "30171", "clock": "1535500907", "value": "21.7000", "ns": "209859168" }, { "itemid": "30171", "clock": "1535500967", "value": "21.7000", "ns": "229296223" } ] 

What is required: Only output key values ​​from all of this. Something like this -

 21.7000, 21.6000, 21.7000 и т.д. 

Then from these values ​​get the minimum, average and maximum. Max. Wed and min. - individual variables. How to implement it?

PS API does not provide the ability to issue only the key value and its values

    3 answers 3

     In [52]: vals = [float(x['value']) for x in data] In [53]: vals Out[53]: [21.6, 21.7, 21.7] 

    minimum:

     In [54]: min(vals) Out[54]: 21.6 

    maximum:

     In [55]: max(vals) Out[55]: 21.7 

    the average:

     In [56]: sum(vals)/len(vals) Out[56]: 21.666666666666668 

    PS In [XXX]: and Out [XXX]: - this is the prompt in IPython (interactive Python) - you do not need to enter them.

    • Gives the error name 'In' is not defined - Gatsby
    • In [XXX]: and Out [XXX]: - this is the prompt in IPython (interactive Python) - no need to drive them - MaxU
    • Works! Thank you so much! - Gatsby

    You can use map ()

     arr = [ { "itemid": "30171", "clock": "1535500848", "value": "21.6000", "ns": "117344023" }, { "itemid": "30171", "clock": "1535500907", "value": "21.7000", "ns": "209859168" }, { "itemid": "30171", "clock": "1535500967", "value": "21.7000", "ns": "229296223" } ] result = list(map(lambda v: v["value"], arr)) print(result) 

      I would recommend

       vals = [float(x.get('value')) for x in data] 

      In case there is somehow no record with the key "value". The code from the answer above is completely working, but we can’t trust everyone to the word.