我想将if dynamic_tickets:
和for ticket in dynamic_tickets:
放在一行。我总是会收到语法错误。您有一个想法,如何实现?
def adjust_prices():
events = Event.objects.filter(status=EventStatus.LIVE)
active_events = [event for event in events if not event.is_over]
for active_event in active_events:
dynamic_tickets = [
ticket for ticket in active_event.tickets.all() if ticket.dynamic_pricing_activated()
]
if dynamic_tickets:
for ticket in dynamic_tickets:
print(ticket)
print("DO OTHER STUFF")
答案 0 :(得分:2)
无需使用'if dynamic_tickets:',因为当列表'dynamic_tickets'将不为空时,仅执行for循环。 只需编写如下代码:
def adjust_prices():
events = Event.objects.filter(status=EventStatus.LIVE)
active_events = [event for event in events if not event.is_over]
for active_event in active_events:
dynamic_tickets = [
ticket for ticket in active_event.tickets.all() if
ticket.dynamic_pricing_activated()
]
for ticket in dynamic_tickets:
print(ticket)
print("DO OTHER STUFF")