In Python, the values come in dictionary form. I want to use the len function to get the largest number, but what should I do?

Asked 2 years ago, Updated 2 years ago, 95 views

I'm coding to get the value I want from Python. First of all, I specified a value for c and r and appended it to shot_list And we used print(len(set(shot_list))) to see the largest number here There are a lot of values, but they come in different lines like below

ex : 
3
3
3
4
5
6

If you simply use print(set(shot_list)) here, dictionary{} It comes in the form of... If print(max(len(set(shot_list)))) is used to extract the largest len value, the TypeError: 'int' object is not usable error occurs.

What I want to do is to select only the largest number of len functions In the ex example, I want to import only 61. What should I do?

    shot_list = []
    mesp_list = []
    real_shot_list = []
    try:
        grid_origin = int(xml_gridshape.find('.//A').text)
    except:
        grid_origin = 0
    xml_measrslts = tree.findall('.//B/C')
    for xml_measrslt in xml_measrslts:
        #axis = int(xml_measrslt.attrib['D'])
        xml_egaphases = xml_measrslt.findall('.//E/F/G/H')
     #H mapのみ
        for elem in xml_egaphases:
            if elem.attrib['I'] == '2':
                xml_egaphase = elem
    shots = xml_egaphase.findall('.//J/K')
    for shot in shots:
        c, r = float(shot.find('.//L/C').text), float(shot.find('.//L/R').text)
        sx, sy = float(shot.find('.//M/X').text), float(shot.find('.//M/Y').text)
        mx, my = float(shot.find('.//N/X').text), float(shot.find('.//N/Y').text)
        d = str(c)+','+str(r)
        shot_list.append(len(d))

        # # print(len(set(shot_list)))
        print(max(shot_list))

python len

2022-09-20 08:47

2 Answers

I've had a long look at what you're talking about. Please explain it more properly.

And set and dict are different.

a = []
For Loop:
    shot_list = []
    shot_list.append(d)
    a.append(len(shot_list))
print(max(a))


2022-09-20 08:47

Adding elements to a list, we're counting the number of unique elements on that list. (3,3,3,4,5,6, it kept increasing, right?) ) You don't have to do the maximum number in the loop, you just have to try once after the for loop.


2022-09-20 08:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.