A safe max() function for empty lists

后端 未结 7 1134
天命终不由人
天命终不由人 2021-02-01 12:28

Evaluating,

max_val = max(a)

will cause the error,

ValueError: max() arg is an empty sequence

Is there a bett

7条回答
  •  南方客
    南方客 (楼主)
    2021-02-01 12:57

    Can create simple lambda to do this:

    get_max = lambda val_list: max([ val for val in val_list if val is not None ]) if val_list else None
    

    You can call it this way:
    get_max(your_list)

提交回复
热议问题