有更有效的方法来执行以下操作吗?我更感兴趣的是知道是否有办法设置“mylist”以匹配任何东西,如果day等于'all',因为在其他情况下,“mylist”可以包含更多元素。
if day == 'all':
mylist = ['monday','tuesday','wednesday','thursday','friday','saturday','sunday']
else:
mylist = [day] # day equal to one of the above
records = meta.Session.query(Transaction).filter(Transaction.day.in_(mylist)).all()
答案 0 :(得分:7)
如果等于'all'
query = meta.session.query(Transaction)
if day != 'all':
query = query.filter(Transaction.day == day)
records = query.all()