Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find greatest item from list in Python

I have below code:

lst = ['2022-12-08 21:53:28.230556_54_12748.png', '2022-12-08 21:53:28.314088_86_14029.png', '2022-12-08 21:53:28.363581_72_12540.png', '2022-12-08 21:53:28.448268_65_13491.png', '2022-12-08 21:53:28.907252_50_7001.png', '2022-12-08 21:53:28.998206_55_13458.png', '2022-12-08 21:53:29.066905_60_8857.png', '2022-12-08 21:53:29.167242_54_8327.png', '2022-12-08 21:53:29.260880_52_11163.png', '2022-12-08 21:53:29.318770_52_7859.png', '2022-12-08 21:53:29.992619_51_7173.png', '2022-12-08 21:53:30.380722_55_7301.png', '2022-12-08 21:53:31.546448_59_7005.png', '2022-12-08 21:53:31.820877_53_6044.png', '2022-12-08 21:53:31.901512_51_5818.png', '2022-12-08 21:53:35.622028_56_7221.png', '2022-12-08 21:53:35.706456_72_7522.png', '2022-12-08 21:53:36.785644_53_8222.png', '2022-12-08 21:53:36.872902_60_10873.png', '2022-12-08 21:53:36.952308_59_10217.png', '2022-12-08 21:53:37.034774_57_7258.png', '2022-12-08 21:53:37.151830_52_7745.png', '2022-12-08 21:53:37.236664_80_13584.png', '2022-12-08 21:53:37.401254_70_13281.png', '2022-12-08 21:53:37.485339_91_16176.png', '2022-12-08 21:53:37.564524_74_13113.png', '2022-12-08 21:53:37.652097_90_13526.png', '2022-12-08 21:53:37.729431_82_13303.png', '2022-12-08 21:53:37.777823_66_12084.png', '2022-12-08 21:53:37.857692_72_14855.png', '2022-12-08 21:53:37.939566_72_16755.png', '2022-12-08 21:53:38.017759_65_13656.png', '2022-12-08 21:53:38.098182_59_14190.png', '2022-12-08 21:53:38.180636_55_13150.png', '2022-12-08 21:53:38.261705_54_11044.png', '2022-12-08 21:53:38.346376_67_11908.png', '2022-12-08 21:53:38.586157_59_11200.png', '2022-12-08 21:53:38.715544_64_10425.png', '2022-12-08 21:53:38.837041_64_12068.png', '2022-12-08 21:53:38.924294_77_13531.png', '2022-12-08 21:53:39.008601_60_10283.png', '2022-12-08 21:53:39.174924_60_10160.png', '2022-12-08 21:53:39.256190_65_10555.png', '2022-12-08 21:53:39.340980_63_10775.png']

runOnce = True
highCon = 0
for img in lst:
    tmp = img.split("_")
    con = tmp[1]
    if runOnce:
        highCon = con
        runOnce = False
    else:
        if con > highCon:
            highCon = con

print(highCon)

lst is a list containing list of filenames. Filename consist of datetime_score_area.png. From the list I have to find the first and second highest score of a file and print it. With above code I am able to print the highest filename but not sure how to find the second highest filename. Can anyone please help. ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can sort the list first:

lst = ['2022-12-08 21:53:28.230556_54_12748.png', '2022-12-08 21:53:28.314088_86_14029.png', '2022-12-08 21:53:28.363581_72_12540.png', '2022-12-08 21:53:28.448268_65_13491.png', '2022-12-08 21:53:28.907252_50_7001.png', '2022-12-08 21:53:28.998206_55_13458.png', '2022-12-08 21:53:29.066905_60_8857.png', '2022-12-08 21:53:29.167242_54_8327.png', '2022-12-08 21:53:29.260880_52_11163.png', '2022-12-08 21:53:29.318770_52_7859.png', '2022-12-08 21:53:29.992619_51_7173.png', '2022-12-08 21:53:30.380722_55_7301.png', '2022-12-08 21:53:31.546448_59_7005.png', '2022-12-08 21:53:31.820877_53_6044.png', '2022-12-08 21:53:31.901512_51_5818.png', '2022-12-08 21:53:35.622028_56_7221.png', '2022-12-08 21:53:35.706456_72_7522.png', '2022-12-08 21:53:36.785644_53_8222.png', '2022-12-08 21:53:36.872902_60_10873.png', '2022-12-08 21:53:36.952308_59_10217.png', '2022-12-08 21:53:37.034774_57_7258.png', '2022-12-08 21:53:37.151830_52_7745.png', '2022-12-08 21:53:37.236664_80_13584.png', '2022-12-08 21:53:37.401254_70_13281.png', '2022-12-08 21:53:37.485339_91_16176.png', '2022-12-08 21:53:37.564524_74_13113.png', '2022-12-08 21:53:37.652097_90_13526.png', '2022-12-08 21:53:37.729431_82_13303.png', '2022-12-08 21:53:37.777823_66_12084.png', '2022-12-08 21:53:37.857692_72_14855.png', '2022-12-08 21:53:37.939566_72_16755.png', '2022-12-08 21:53:38.017759_65_13656.png', '2022-12-08 21:53:38.098182_59_14190.png', '2022-12-08 21:53:38.180636_55_13150.png', '2022-12-08 21:53:38.261705_54_11044.png', '2022-12-08 21:53:38.346376_67_11908.png', '2022-12-08 21:53:38.586157_59_11200.png', '2022-12-08 21:53:38.715544_64_10425.png', '2022-12-08 21:53:38.837041_64_12068.png', '2022-12-08 21:53:38.924294_77_13531.png', '2022-12-08 21:53:39.008601_60_10283.png', '2022-12-08 21:53:39.174924_60_10160.png', '2022-12-08 21:53:39.256190_65_10555.png', '2022-12-08 21:53:39.340980_63_10775.png']
lst.sort(key= lambda x: int(x.split("_")[1]),reverse=True)
print(lst[0]) # Highest score filename
print(lst[1]) # Second highest filename

If you don’t want to change the order of the original list you can use sorted as well:

highest, nxt_highest = sorted(lst, key=lambda x: int(x.split("_")[1]),reverse=True)[:2]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading