How can I check that a list has one and only one truthy value?

后端 未结 17 2466
暗喜
暗喜 2020-11-27 12:23

In python, I have a list that should have one and only one truthy value (that is, bool(value) is True). Is there a clever way to check for this

17条回答
  •  Happy的楠姐
    2020-11-27 13:21

    If there is only one True, then the length of the Trues should be one:

    def only_1(l): return 1 == len(filter(None, l))
    

提交回复
热议问题