Python 使用具有不同过滤条件的列表理解
有一个函数将参数作为布尔值Python 使用具有不同过滤条件的列表理解,python,Python,有一个函数将参数作为布尔值 def get_choices(retirement): choices = [ { 'id': id, 'name': CommonName.build_item_name( data ) } for cd, data in _collection.items() if not is_used_ite
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筛选如果未使用项目(cd)
在retirement=True
时不被应用?添加是否使用项目(cd)
,因此如果retirement
是True
则条件短路,并且是否使用项目
不被调用/评估
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)
]
添加
报废与否\u项目(cd)
,因此如果报废
为真
则条件短路且已使用\u项目
不被调用/评估
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)
]