Find maximum int value from list which contain both str and int python

前端 未结 5 1779

Looking to find max from the combine list as follows:

[\'filename1\', 1696, \'filename2\', 5809,....]

I have tried following:<

5条回答
  •  攒了一身酷
    2020-12-11 15:07

    You can also use try...except clause.

    lst = ['filename1', 1696, 'filename2', 5809]
    numbers = []
    
    for item in lst:
        try:
            numbers.append(int(item))
        except ValueError:
            pass # Ignore items which are not numbers
    
    print(max(numbers))
    # 5809
    

提交回复
热议问题