I have seen numerous posts on this topic, but none have worked for me. For this reason I am asking my own question specific to what I am trying to do. The code I have set up is below.
def get_assets_for_group(ip):
decom_match = str(ip)
url5 = server + path1
response = requests.request("GET", url5, headers=headers, verify=False)
data = response.json()
for i in data['resources']:
url = server+path2+str(i)
response = requests.request("GET", url, headers=headers, verify=False)
data = response.json()
if decom_match in data["ip"]:
d = {i}
max_value = max(d)
print("Match found!", max_value)
The output of this code will give me all the matching values when I only want it to return the one with the highest number. An example of the output is below.
Match found! 111618
Match found! 112367
Match found! 115401
Match found! 115618
Match found! 116265
Match found! 116400
Match found! 117653
Am I using the max function wrong? Please let me know what you think or possible fixes.
The issue is that you are applying the max()
function on a set containing only i
- and then printing the result - every time you find a matching value. One way to rectify this is to create an initial set, matches
, and then every time you find a match, add it to that set. After you've finished searching the response for matches, you can then use max()
on this set and print the result.
Something like this:
def get_assets_for_group(ip):
decom_match = str(ip)
url5 = server + path1
response = requests.request("GET", url5, headers=headers, verify=False)
data = response.json()
matches = set()
for i in data['resources']:
url = server+path2+str(i)
response = requests.request("GET", url, headers=headers, verify=False)
data = response.json()
if decom_match in data["ip"]:
matches.add(i)
if matches:
print("Match found!", max(matches))