for循环有3次迭代(并且会更快)。是否可以在第二个循环之后为assign_organizer定义标准值?
[...]
name = ['Super Bowl 2018', 'Jon Summer 2019', 'Jon Summer 2020', ]
slug = ['super-bowl-2018', 'jon-summer-2019', 'jon-summer-2020', ]
first_organizer = Organizer.objects.all().first()
last_organizer = Organizer.objects.all().last()
assign_organizer = [first_organizer, last_organizer, ]
for i in range(len(name)):
Event.objects.create(
organizer=assign_organizer[i],
name=name[i],
slug=slug[i],
status='on_sale',
short_description=lorem,
additional_information=lorem_long,
venue_name=lorem,
venue_address=lorem,
start_date='2018-05-26 18:12:58.556925+02',
end_date='2018-05-26 18:12:58.556925+02',
facebook_event_link=lorem,
banner=lorem,
)
__更新__:
我现在尝试了评论中提出的解决方案,但是,我没有得到预期的结果。 (我现在没有违约,只是决定从头开始做起来更好。)
预期:
测试1:A 测试2:1 已分配:a1
测试1:B 测试2:2 已分配:a2
测试1:C 测试2:3 已分配:a1
实际结果: 但是,它会在第二次循环后停止。
test1 = ['A', 'B', 'C']
test2 = ['1', '2', '3']
assigned_raw = ['a1', 'a2']
assigned = itertools.chain(assigned_raw)
for a, b, c in zip(test1, test2, assigned):
print("TEST 1: ", a)
print("TEST 2: ", b)
print("Assigned: ", c)