[Python] Pythonic Methods - Is there a more Pythonic way to display this information, I'd like...

Discussão em 'Python' iniciado por Stack, Maio 23, 2025.

  1. Stack

    Stack Membro Participativo

    Below I am using an API call, getting data, finding the top 10 matches, then printing the top 10 match information (this part has got to be a more pythonic way) after that I calculate the average accuracy of the the matches.

    I do this three different times each time using different data (this part has got to be a more pythonic way)

    I don't want to use loops because I know they are time consuming.....any ideas?

    '''
    url = 'xxx’ # getting data from an api
    data = response.json()
    # print(data)
    # data is a list of dictionaries in the format:
    # [{key01:value01, key11:value11, key21:value21, key31:value31 … key71:value71},
    # [{key02:value02, key12:value12, key22:value22, key32:value32 … key72:value72},…
    # [{key015:value015, key115:value115, key215:value215, key315:value315 … key715:value715}]
    # -> 15 dictionaries each containing 8 lists of a key and value pair.

    # Create a flat list of return values without keys
    # Keys can be seen in the list of dictionaries in the data file.
    b = [] # create an empty list
    for i in data:
    for values in i.values():
    b.append(values)
    # print("list B:", b)

    print("The top ten matches are:")
    print(b[1], "matches:", b[5], "with accuracy:", b[7])
    print(b[9], "matches:", b[13], "with accuracy:", b[15])
    print(b[17], "matches:", b[21], "with accuracy:", b[23])
    print(b[25], "matches:", b[29], "with accuracy:", b[31])
    print(b[33], "matches:", b[37], "with accuracy:", b[39])
    print(b[41], "matches:", b[45], "with accuracy:", b[47])
    print(b[49], "matches:", b[53], "with accuracy:", b[55])
    print(b[57], "matches:", b[61], "with accuracy:", b[63])
    print(b[65], "matches:", b[69], "with accuracy:", b[71])
    print(b[73], "matches:", b[77], "with accuracy:", b[79], "\n")

    hacc = []
    indices_to_add = [7, 15, 23, 31, 39, 47, 55, 63, 71, 79]
    for index in indices_to_add:
    if index < len(b):
    hacc.append(b[index])
    # print(indices_to_add)
    # print(hacc)

    # Find the average confidence value between the top 10
    havg = sum(hacc) / len(hacc)
    print("The average accuracy is: {:0.2f}".format(havg), "\n \n")

    #
    #****************
    # API Call

    url = 'xxx’
    response = requests.get(url)
    mdata = response.json()

    # Creates a flat list of the return values without keys
    # Keys can be seen in the list of dictionaries in the mdata file.
    c = [] # create an empty list
    for i in mdata:
    for values in i.values():
    c.append(values)
    # print("list C:", c)

    print("The top ten matches are: \n")
    print(c[1], "matches:", c[5], "with accuracy:", c[7])
    print(c[9], "matches:", c[13], "with accuracy:", c[15])
    print(c[17], "matches:", c[21], "with accuracy:", c[23])
    print(c[25], "matches:", c[29], "with accuracy:", c[31])
    print(c[33], "matches:", c[37], "with accuracy:", c[39])
    print(c[41], "matches:", c[45], "with accuracy:", c[47])
    print(c[49], "matches:", c[53], "with accuracy:", c[55])
    print(c[57], "matches:", c[61], "with accuracy:", c[63])
    print(c[65], "matches:", c[69], "with accuracy:", c[71])
    print(c[73], "matches:", c[77], "with accuracy:", c[79], "\n")

    macc = []
    indices_to_add = [7, 15, 23, 31, 39, 47, 55, 63, 71, 79]
    for index in indices_to_add:
    if index < len(c):
    macc.append(c[index])
    # print(macc)

    # Find the average accuracy of the top 10
    mavg = sum(macc) / len(macc)
    print("The average accuracy of the top 10 matches is:{:0.2f}".format(mavg), "\n \n")

    #
    #****************
    # API Call

    url = 'xxx'
    response = requests.get(url)
    idata = response.json()

    # Creates a flat list of the return values without keys
    # Keys can be seen in the list of dictionaries in the idata file.
    d = [] # create an empty list
    for i in idata:
    for values in i.values():
    d.append(values)
    # print("list D:", d)

    print("The top ten matches are: \n")
    print(d[1], "matches:", d[5], "with accuracy:", d[7])
    print(d[9], "matches:", d[13], "with accuracy:", d[15])
    print(d[17], "matches:", d[21], "with accuracy:", d[23])
    print(d[25], "matches:", d[29], "with accuracy:", d[31])
    print(d[33], "matches:", d[37], "with accuracy:", d[39])
    print(d[41], "matches:", d[45], "with accuracy:", d[47])
    print(d[49], "matches:", d[53], "with accuracy:", d[55])
    print(d[57], "matches:", d[61], "with accuracy:", d[63])
    print(d[65], "matches:", d[69], "with accuracy:", d[71])
    print(d[73], "matches:", d[77], "with accuracy:", d[79], "\n")

    iacc = []
    indices_to_add = [7, 15, 23, 31, 39, 47, 55, 63, 71, 79]
    for index in indices_to_add:
    if index < len(d):
    iacc.append(d[index])

    # Find the average accuracy of the top 10
    iavg = sum(iacc) / len(iacc)
    print("The average accuracy of the top 10 matches is:{:0.2f}".format(iavg), "\n \n")'''

    Continue reading...

Compartilhe esta Página