-1

In Python, is there a good way (programmatically) to get the value of ["win"]["amount"] from the subset of data where the most recent date exists?

To provide a more concrete example of what I'm asking, I'd like the amount for win from April 2, 2018 (2018-04-02), which would be 199.51.

This is the source JSON (I convert this to a Python dict using json.loads):

{
    "numbers": [{
        "lose": {
            "amount": "122.50"
        },
        "win": {
            "amount": "232.50"
        },
        "date": "2018-01-08"
    }, {
        "lose": {
            "amount": "233.75"
        },
        "win": {
            "amount": "216.25"
        },
        "date": "2018-03-05"
    }, {
        "lose": {
            "amount": "123.50"
        },
        "win": {
            "amount": "543.00"
        },
        "date": "2018-03-12"
    }, {
        "lose": {
            "amount": "213.31"
        },
        "win": {
            "amount": "253.33"
        },
        "date": "2018-03-19"
    }, {
        "lose": {
            "amount": "217.00"
        },
        "win": {
            "amount": "199.51"
        },
        "date": "2018-04-02"
    }]
}

This seems like a very simple solution is in order, but I cannot quite nail down what that solution is, or if there is a Pythonic way of accomplishing this. I did write some logic to calculate the largest date by putting all of the dates into a list called datelist and doing a max(datelist), but I'm not sure how to relate that back to get ["amount"]["win"].

  • Can you demonstrate *any* effort at solving this yourself? – Scott Hunter Apr 09 '18 at 00:31
  • I'm going to respond to that question objectively: Yes, when I put the dates into `datelist` and calculated the most recent using `max()`, as stated. But, I'm not entirely sure how to relate that calculation back to getting the `amount` I am trying to get. I was thinking the best thing to do would be to somehow search the `dict` by value instead of key. I tried something like this, but to no avail (I think because date is "outside" of `win` and `lose`) https://stackoverflow.com/questions/8023306/get-key-by-value-in-dictionary – stuntmachine Apr 09 '18 at 00:37
  • The solution from @juanpa-arrivillaga (https://stackoverflow.com/a/49724192/9616920) worked for me here. – stuntmachine Apr 09 '18 at 00:52

2 Answers2

0

Given that your dates are in ISO 8601 notation you can do simple string compare as a sorting mechanism for your inner numbers list and then pick up the last element:

import operator

oldest = sorted(your_data["numbers"], key=operator.itemgetter("date"))[0]["win"]["amount"]
# 232.50
newest = sorted(your_data["numbers"], key=operator.itemgetter("date"))[-1]["win"]["amount"]
# 199.51

Or just sort the inner list once and then pick up whichever element you want if you need to access them more often.

zwer
  • 24,943
  • 3
  • 48
  • 66
0

If the 'date' attributes follow ISO 8601 so the lexicographical order corresponds to the chronological order then you can simply use max with an appropriate key, so:

In [12]: max(data['numbers'], key=lambda d:d['date'])['win']['amount']
Out[12]: '199.51'
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172