代码之家  ›  专栏  ›  技术社区  ›  Jekson

使用不同过滤条件的列表理解

  •  0
  • Jekson  · 技术社区  · 4 年前

    有一个函数将参数作为布尔值

    def get_choices(retirement):
            choices = [
            {
                'id': id,
                'name': CommonName.build_item_name(
                    data
                )
            } for cd, data in _collection.items()
            if not is_used_item(cd)
        ]
    

    retirement 价值观。是否可以使列表comp筛选 if not_used_item(cd) retirement = True ?

    1 回复  |  直到 4 年前
        1
  •  2
  •   DeepSpace    4 年前

    添加 retirement or not is_used_item(cd) ,所以如果 retirement True 条件短路,并且 is_used_item

    def get_choices(retirement):
            choices = [
            {
                'id': id,
                'name': CommonName.build_item_name(
                    data
                )
            } for cd, data in _collection.items()
            if retirement or not is_used_item(cd)
        ]