Home > Blockchain >  How to return the last 5 values that exist before the last and penultimate in a JSON?
How to return the last 5 values that exist before the last and penultimate in a JSON?

Time:04-23

At the moment for me to be able to do this, I get the last 7 values, then I create a list with the first 5:

last_seven = response['graphPoints'][-7:]
only_five = [last_seven[0],last_seven[1],last_seven[2],last_seven[3],last_seven[4]]

As I'm still learning, I had to do it in this archaic way because I couldn't understand how I can get these 5 directly from ([-7:]) JSON minus the last and penultimate one, I would like some help to do it correctly.

My expected colect on this example are:

    {
      "minute": 33,
      "value": 42
    },
    {
      "minute": 34,
      "value": 28
    },
    {
      "minute": 35,
      "value": 16
    },
    {
      "minute": 36,
      "value": -30
    },
    {
      "minute": 37,
      "value": -22
    }

To make it easier, I leave here an example JSON in case you want to test it yourself:

{
  "graphPoints": [
    {
      "minute": 1,
      "value": 0
    },
    {
      "minute": 2,
      "value": 0
    },
    {
      "minute": 3,
      "value": 5
    },
    {
      "minute": 4,
      "value": 8
    },
    {
      "minute": 5,
      "value": 25
    },
    {
      "minute": 6,
      "value": 65
    },
    {
      "minute": 7,
      "value": 39
    },
    {
      "minute": 8,
      "value": 23
    },
    {
      "minute": 9,
      "value": -25
    },
    {
      "minute": 10,
      "value": -9
    },
    {
      "minute": 11,
      "value": -39
    },
    {
      "minute": 12,
      "value": -24
    },
    {
      "minute": 13,
      "value": -14
    },
    {
      "minute": 14,
      "value": -7
    },
    {
      "minute": 15,
      "value": 60
    },
    {
      "minute": 16,
      "value": 36
    },
    {
      "minute": 17,
      "value": 22
    },
    {
      "minute": 18,
      "value": 8
    },
    {
      "minute": 19,
      "value": 10
    },
    {
      "minute": 20,
      "value": 7
    },
    {
      "minute": 21,
      "value": 4
    },
    {
      "minute": 22,
      "value": 8
    },
    {
      "minute": 23,
      "value": 5
    },
    {
      "minute": 24,
      "value": 3
    },
    {
      "minute": 25,
      "value": 2
    },
    {
      "minute": 26,
      "value": 61
    },
    {
      "minute": 27,
      "value": 41
    },
    {
      "minute": 28,
      "value": 35
    },
    {
      "minute": 29,
      "value": 51
    },
    {
      "minute": 30,
      "value": 40
    },
    {
      "minute": 31,
      "value": 20
    },
    {
      "minute": 32,
      "value": 72
    },
    {
      "minute": 33,
      "value": 42
    },
    {
      "minute": 34,
      "value": 28
    },
    {
      "minute": 35,
      "value": 16
    },
    {
      "minute": 36,
      "value": -30
    },
    {
      "minute": 37,
      "value": -22
    },
    {
      "minute": 38,
      "value": -43
    },
    {
      "minute": 39,
      "value": -26
    }
  ],
  "periodTime": null,
  "periodCount": 2
}

CodePudding user response:

You want to get the first 5 values of the last seven values.

This can be done in two ways:

response['graphPoints'][-7:][5:]

Explaining the code above: First you get the last 7 values as a list. Then by typing [5:] you get the first 5 values of the result.

Better way

BUT there is a better way. You can do this in one indexing:

response['graphPoints'][-7:-2]

This way you tell python to give you the values that their indexes are -7, -6, -5, -4 and -3. Note that -2 is not in the range bacause the number after : is not in the results so it goes to before index -2 that is index -3.

I tested all these ways on your data and it works perfectly.

  • Related