compare two python strings that contain numbers

前端 未结 5 1512
礼貌的吻别
礼貌的吻别 2020-12-19 11:29

UPDATE: I should have specified this sooner, but not all of the names are simply floats. For example, some of them are \"prefixed\" with \"YT\". So for example\" YT1.1. so,

5条回答
  •  借酒劲吻你
    2020-12-19 12:27

    This is not a built-in method, but it ought to work:

    >>> def lt(num1, num2):
    ...     for a, b in zip(num1.split('.'), num2.split('.')):
    ...         if int(a) < int(b):
    ...             return True
    ...         if int(a) > int(b):
    ...             return False
    ...     return False
    ... 
    ... lt('4.2', '4.11')
    0: True
    

    That can be cleaned up, but it gives you the gist.

提交回复
热议问题