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

前端 未结 5 1781

Looking to find max from the combine list as follows:

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

I have tried following:<

5条回答
  •  死守一世寂寞
    2020-12-11 14:51

    The same isinstance idea can be applied to a filter:

    f = ['filename1', 1696, 'filename2', 5809]
    max(filter(lambda i: isinstance(i, int), f))
    

    Also, if you need to include more than one data type in your comparison, e.g.: floats, you can simple use a tuple to validate the the data to be compared:

    max(filter(lambda i: isinstance(i, (int, float)), f))
    

提交回复
热议问题