#๐Ÿ”’ How to get number from Json string and get closest number below given number?

24 messages ยท Page 1 of 1 (latest)

ripe glacier
#

If I have Json below:

{
"ScriptMethod": [
{
"Address": 10000000,
"Name": "Interop.ErrorInfo$$.ctor",
"Signature": "void Interop_ErrorInfo___ctor (Interop_ErrorInfo_o __this, int32_t errno, const MethodInfo* method);",
"TypeSignature": "viii"
},
{
"Address": 20000000,
"Name": "Interop.ErrorInfo$$.ctor",
"Signature": "void Interop_ErrorInfo___ctor (Interop_ErrorInfo_o __this, int32_t error, const MethodInfo* method);",
"TypeSignature": "viii"
},
{
"Address": 30000000,
"Name": "Interop.ErrorInfo$$get_Error",
"Signature": "int32_t Interop_ErrorInfo__get_Error (Interop_ErrorInfo_o __this, const MethodInfo* method);",
"TypeSignature": "iii"
},
{
"Address": 40000000,
"Name": "Interop.ErrorInfo$$get_RawErrno",
"Signature": "int32_t Interop_ErrorInfo__get_RawErrno (Interop_ErrorInfo_o __this, const MethodInfo* method);",
"TypeSignature": "iii"
},
{
"Address": 50000000,
"Name": "Interop.ErrorInfo$$GetErrorMessage",
"Signature": "System_String_o* Interop_ErrorInfo__GetErrorMessage (Interop_ErrorInfo_o __this, const MethodInfo* method);",
"TypeSignature": "iii"
},
{
"Address": 60000000,
"Name": "Interop.ErrorInfo$$ToString",
"Signature": "System_String_o* Interop_ErrorInfo__ToString (Interop_ErrorInfo_o __this, const MethodInfo* method);",
"TypeSignature": "iii"
}
}
}

How can I get Address above as a List or Array or anything, then
If I have given number 33000000

How can I compare my given number with the list of Address, then find closest number below my given number

I mean, I want the result to be 30000000

lofty driftBOT
#

@ripe glacier

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

indigo belfry
#

If you keep it sorted, then you could do a binary search, then at the end either you find an exact match, and use that, or you just calculate the abs difference between the target and left/right node, pick whichever one is closer

#

Otherwise you could also just loop through each one, calculate the abs difference between the target and the element you're checking, and store the minimum difference along with the index

#

Then just access that index

ripe glacier
#

@indigo belfry
what code or command or function should I use to get the address as a list or array?

then, is it possible to do like this below?

myArr[myArr < myNumber].max()

zenith dragon
#

!e ```py
def func(values, target):
return max(sorted(v for v in values if v < target)) # <= ?

json_data = {
"ScriptMethod": [
{
"Address": 10000000,
"Name": "Interop.ErrorInfo$$.ctor",
"Signature": "void Interop_ErrorInfo_ctor (Interop_ErrorInfo_o this, int32_t errno, const MethodInfo* method);",
"TypeSignature": "viii"
},
{
"Address": 20000000,
"Name": "Interop.ErrorInfo$$.ctor",
"Signature": "void Interop_ErrorInfo_ctor (Interop_ErrorInfo_o this, int32_t error, const MethodInfo* method);",
"TypeSignature": "viii"
},
{
"Address": 30000000,
"Name": "Interop.ErrorInfo$$get_Error",
"Signature": "int32_t Interop_ErrorInfoget_Error (Interop_ErrorInfo_o this, const MethodInfo* method);",
"TypeSignature": "iii"
},
{
"Address": 40000000,
"Name": "Interop.ErrorInfo$$get_RawErrno",
"Signature": "int32_t Interop_ErrorInfoget_RawErrno (Interop_ErrorInfo_o this, const MethodInfo* method);",
"TypeSignature": "iii"
},
{
"Address": 50000000,
"Name": "Interop.ErrorInfo$$GetErrorMessage",
"Signature": "System_String_o* Interop_ErrorInfoGetErrorMessage (Interop_ErrorInfo_o this, const MethodInfo* method);",
"TypeSignature": "iii"
},
{
"Address": 60000000,
"Name": "Interop.ErrorInfo$$ToString",
"Signature": "System_String_o* Interop_ErrorInfoToString (Interop_ErrorInfo_o this, const MethodInfo* method);",
"TypeSignature": "iii"}
]
}

values = [d['Address'] for d in json_data['ScriptMethod']]

result = func(values, 33000000)
print(result)```

lofty driftBOT
zenith dragon
#

@ripe glacierSomething along these lines?

#

Actually...I don't know why I sorted it.

#

I was thinking of something else.

#

Hang on.

#

It still works, it's just...extra.

ripe glacier
#

@zenith dragon
Wow, Thank You

Ok, I'm waiting

zenith dragon
#

So you definitely want the value that's lower than the target, not lower to or equal to the target?

#

!e ```py
def func(values, target):
return max(v for v in values if v < target)

result = func([1, 3, 5], 3)
print(result)```

lofty driftBOT
zenith dragon
#

vs

#

!e ```py
def func(values, target):
return max(v for v in values if v <= target)

result = func([1, 3, 5], 3)
print(result)```

lofty driftBOT
zenith dragon
#

@ripe glacier

lofty driftBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.