omg21
V2EX  ›  问与答

有 None 的数据里怎么排序?

  •  
  •   omg21 · Jul 10, 2016 · 1803 views
    This topic created in 3664 days ago, the information mentioned may be changed or developed.

    我现在处理好 5 个数据,分别存在 a1,a2...a5 里,举个例子: a1=32 a2=None a3=5 a4=10 a5=None

    我现在想做个排序,想取最小值 5 ,应该怎么做?

    6 replies    2016-07-10 19:03:49 +08:00
    hcwhan
        1
    hcwhan  
       Jul 10, 2016 via Android
    不同语言都不一样
    hcwhan
        2
    hcwhan  
       Jul 10, 2016 via Android
    最简单的 比较前做下类型判断
    htfy96
        3
    htfy96  
       Jul 10, 2016
    sorted(filter(lambda x: x is not None, [32, None, 5, 10, None]))
    omg21
        4
    omg21  
    OP
       Jul 10, 2016
    @htfy96 lambda 好强大
    strongcoder
        5
    strongcoder  
       Jul 10, 2016
    这个挺简单吧,先把 none 剔除掉,然后两两比较取小的
    shyling
        6
    shyling  
       Jul 10, 2016
    reduce(lambda a,b: a if b is None else a if a<b else b,[32,None,5,10,None])
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3569 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 00:40 · PVG 08:40 · LAX 17:40 · JFK 20:40
    ♥ Do have faith in what you're doing.