简体   繁体   中英

How to print only the max value of an ouput

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))

You have a couple of problems in your code. You iterate over your data, create a new set in each iteration and apply the max function of this new set with only one entry, therefore it is always the one entry, that is in your set. You should either fill the set with all found matches and apply the max() function afterwards, or check in every iteration, if a new max value has been found:

    max_value = None
    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"]:
            if max_value == None:
                max_value = i
            else:
                max_value = max(i, max_value)
    if max_value != None:
        print("Match found!", max_value)
    return max_value

It's hard to tell what you're doing without example data but it looks like you're running max on a single value every time in the loop.

Define a variable outside the loops containing the maximum found value and check inside the loop if the current value is higher like so:

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()

    max_value = 0 # initialize max value
    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}
        # check if value is higher than last found value
        if (d > max_value):
            max_value = d

    print("Match found!", max_value)

Is it a matter of calculating the max after your for loop finished perhaps? Also put all values in a list first.

...
d=[]
    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.append(i)

max_value = max(d)
print("Match found!", max_value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM