1

How do I return the index for the array element whose sub-element value is the closest matching Int to my query.

eg: If the array looks like this:

{
  "data": [
      {
        "time": 1483304400,
      },
      {
        "time": 1483308000,
      },
      {
        "time": 1483311600,
      },
      {
        "time": 1483315200,
      },
      {
        "time": 1483318800,
      }
    ]
}

and my query is 1483311700 then I want to return 2 as that's the index for the closest matching element.

1
  • this is a dictionary , you can look it like a json , use a library like demjson , then decode it using this instruction , demjson.decode(String that containing the input) then look it like an array , use a for loop , then calculate the most close number to your expected number Commented Jan 1, 2017 at 22:43

3 Answers 3

8

This can be done using the built-in min function with a custom key function that returns the absolute difference between the value of a 'time' key and query.

min(it, key=keyfunc) returns the smallest element from it according to keyfunc. If you use enumerate(it) instead of it and tweak the key function accordingly, min will return both the element and its index:

>>> index, value = min(enumerate(data), key=lambda x:abs(x[1]['time'] - query))
>>> index
2

Here data is the value of the 'data' key in your input.

Sign up to request clarification or add additional context in comments.

Comments

1

Simple solution using bisect.bisect(a, x, lo=0, hi=len(a)) function(returns an insertion point which comes after (to the right of) any existing entries of x in a.):

idx = bisect.bisect([d['time'] for d in data], 1483311700) - 1
print(idx)  # 2

https://docs.python.org/3/library/bisect.html#module-bisect

Comments

0

Isolate the time values in a list.

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:data = {
:  "data": [
:      {
:        "time": 1483304400,
:      },
:      {
:        "time": 1483308000,
:      },
:      {
:        "time": 1483311600,
:      },
:      {
:        "time": 1483315200,
:      },
:      {
:        "time": 1483318800,
:      }
:    ]
:}
:--

In [2]: data["data"]
Out[2]: 
[{'time': 1483304400},
 {'time': 1483308000},
 {'time': 1483311600},
 {'time': 1483315200},
 {'time': 1483318800}]

In [3]: times = [j["time"] for j in data["data"]]

In [4]: times
Out[4]: [1483304400, 1483308000, 1483311600, 1483315200, 1483318800]

Then generate a list of the absolute difference between the list items and your query.

In [3]: times = [j["time"] for j in data["data"]]

In [4]: times
Out[4]: [1483304400, 1483308000, 1483311600, 1483315200, 1483318800]

In [5]: diff = [abs(j-1483311700) for j in times]

In [6]: diff
Out[6]: [7300, 3700, 100, 3500, 7100]

Last, find the index of the lowest difference.

In [7]: min(diff)
Out[7]: 100

In [8]: diff.index(100)
Out[8]: 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.